allform.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /**
  2. * Created by Administrator on 2021/5/14.
  3. */
  4. var cur_open_div;
  5. var child_index;
  6. var layer = layui.layer;
  7. layui.use(['jquery', 'layer','form','laydate'], function(){
  8. $ = layui.$;
  9. var form = layui.form;
  10. var laydate = layui.laydate;
  11. form.on('submit(formDemo)', function(data){
  12. var loadingIndex = layer.load();
  13. $.ajax({
  14. url: data.form.action,
  15. type: data.form.method,
  16. data: data.field,
  17. dataType:'json',
  18. headers: { 'X-CSRF-Token' : $('meta[name=_token]').attr('content') },
  19. success: function (info) {
  20. console.log(data.field);
  21. layer.close(loadingIndex);
  22. if(info.status == 0)
  23. {
  24. layer.msg(info.result.message,{icon: 1,time: 2000});
  25. }else if(info.status == 1){
  26. var go_url = location.href;
  27. if( info.result.hasOwnProperty("url") )
  28. {
  29. go_url = info.result.url;
  30. }
  31. layer.msg('操作成功',{time: 1000,
  32. end:function(){
  33. location.href = info.result.url;
  34. }
  35. });
  36. }
  37. }
  38. });
  39. return false;
  40. });
  41. $('.form-config').delegate(".deleteBtn","click",function(){
  42. $(this).parent().parent().parent().remove();
  43. var random_code = $(this).attr('random_code');
  44. $('#phone_item_'+random_code).remove();
  45. if($('.phone-item').length > 0){
  46. $('.no_element').hide();
  47. }else{
  48. $('.no_element').show();
  49. }
  50. });
  51. $('.form-config').delegate(".delOptionBtn","click",function(){
  52. var obj = $(this).parent().parent().parent();
  53. $(this).parent().parent().remove();
  54. var random_code = $(this).attr('random_code');
  55. var num = $(this).attr('num');
  56. updateOptionSort(obj);
  57. $('#radio-item'+num+'_'+random_code).remove();
  58. });
  59. $('.form-config').delegate(".addOptionBtn","click",function(){
  60. $(this).parent().parent().find('.delOptionBtn').removeClass("disabled");
  61. var random_code = $(this).attr('random_code');
  62. var sort = $(this).parent().parent().find('.sort');
  63. var type = $(this).attr('type');
  64. var num = sort.length + 1;
  65. if(type == 'radio'){
  66. var html = '<div class="input-middle-item">'
  67. + '<div class="input-group">'
  68. + '<div class="input-group-addon">选项<span class="sort">'+num+'</span></div>'
  69. + '<input class="form-control" name="option_val_'+random_code+'[]" type="text" placeholder="请输入" value="选项'+num+'" style="width: 430px;" update_id="#radio-item'+num+'_'+random_code+'" update_type="radio">&nbsp;'
  70. + '<a class="delOptionBtn" href="javascript:;" num="'+num+'" random_code="'+random_code+'">删除</a></div></div>';
  71. var phone_html = '<div class="radio-item" id="radio-item'+num+'_'+random_code+'">'
  72. + '<input type="radio" class="radio_option" name="radio" value="选项'+num+'" title="选项'+num+'">'
  73. + '</div>';
  74. $(this).parent().before(html);
  75. $('#m-radio-box_'+random_code).append(phone_html);
  76. }if(type == 'select'){
  77. var html = '<div class="input-middle-item">'
  78. + '<div class="input-group">'
  79. + '<div class="input-group-addon">选项<span class="sort">'+num+'</span></div>'
  80. + '<input class="form-control" name="option_val_'+random_code+'[]" type="text" placeholder="请输入" value="选项'+num+'" style="width: 430px;" update_id="#radio-item'+num+'_'+random_code+'" update_type="radio">&nbsp;'
  81. + '<a class="delOptionBtn" href="javascript:;" num="'+num+'" random_code="'+random_code+'">删除</a></div></div>';
  82. $(this).parent().before(html);
  83. }else if(type == 'checkbox'){
  84. var html = '<div class="input-middle-item">'
  85. + '<div class="input-group">'
  86. + '<div class="input-group-addon">选项<span class="sort">'+num+'</span></div>'
  87. + '<input class="form-control" name="option_val_'+random_code+'[]" type="text" placeholder="请输入" value="选项'+num+'" style="width: 430px;" update_id="#radio-item'+num+'_'+random_code+'" update_type="radio">&nbsp;'
  88. + '<a class="delOptionBtn" href="javascript:;" num="'+num+'" random_code="'+random_code+'">删除</a></div></div>';
  89. var phone_html = '<div class="radio-item" id="radio-item'+num+'_'+random_code+'">'
  90. + ' <input type="checkbox" class="radio_option" name="checkbox" title="选项'+num+'" value="选项'+num+'" lay-skin="primary">'
  91. + '</div>';
  92. $(this).parent().before(html);
  93. $('#m-radio-box_'+random_code).append(phone_html);
  94. }
  95. form.render();
  96. })
  97. $('.act_btn').click(function(){
  98. var type = $(this).attr('type');
  99. var loadingIndex = layer.load();
  100. $.ajax({
  101. url: add_item_url,
  102. data:{type : type},
  103. dataType: 'json',
  104. success:function(result)
  105. {
  106. var data = result.result.data;
  107. var random_code = data.random_code;
  108. var item_li = data.item_li;
  109. var phone_item = data.phone_item;
  110. layer.close(loadingIndex);
  111. $('#form_config_ul').append(item_li);
  112. $('.phone-main-content').append(phone_item);
  113. form.render(null, 'phone_item_'+random_code);
  114. form.render(null, 'action_li_'+random_code);
  115. if($('.phone-item').length > 0){
  116. $('.no_element').hide();
  117. }else{
  118. $('.no_element').show();
  119. }
  120. }
  121. })
  122. })
  123. $('.form-config').delegate(".action-items input","input propertychange",function(){
  124. var val = $(this).val();
  125. var update_id = $(this).attr('update_id');
  126. var type = $(this).attr('update_type');
  127. if(type == 'html'){
  128. $(update_id).html(val);
  129. }else if(type == 'value'){
  130. $(update_id).val(val);
  131. }else if(type == 'tip'){
  132. $(update_id).attr('placeholder', val);
  133. }else if(type == 'select_tip'){
  134. $(update_id).attr('placeholder', val);
  135. form.render();
  136. $(update_id).parent().find('.layui-select-title').find('.layui-input').attr('placeholder', val);
  137. }else if(type == 'select_area_tip'){
  138. var random_code = $(this).attr('random_code');
  139. var province_id = $('select[name="province_id_'+random_code+'"]').find("option:selected").val();
  140. if(province_id == '' || province_id.indexOf("请选择") >= 0){
  141. $(update_id).attr('placeholder', val);
  142. $(update_id).parent().find('.layui-select-title').find('.layui-input').attr('placeholder', val);
  143. }
  144. }else if(type == 'radio'){
  145. $(update_id).find('.radio_option').attr('title', val);
  146. $(update_id).find('.radio_option').val(val);
  147. form.render();
  148. }
  149. });
  150. })
  151. //更新选项排序
  152. function updateOptionSort(obj){
  153. var sort = obj.find('.sort');
  154. var count = sort.length;
  155. var num = 1;
  156. obj.find('.sort').each(function(){
  157. $(this).html(num);
  158. num = num + 1;
  159. if(count == 1){
  160. $(this).parent().parent().find('.delOptionBtn').addClass("disabled");
  161. }
  162. })
  163. }
  164. //更新地址信息
  165. function updateArea(random_code){
  166. var area_type = $('input[name="area_type_'+random_code+'"]:checked').val();
  167. var province_id = $('select[name="province_id_'+random_code+'"]').find("option:selected").val();
  168. var province_text = $('select[name="province_id_'+random_code+'"]').find("option:selected").text();
  169. var city_id = $('select[name="city_id_'+random_code+'"]').find("option:selected").val();
  170. var city_text = $('select[name="city_id_'+random_code+'"]').find("option:selected").text();
  171. var country_id = $('select[name="country_id_'+random_code+'"]').find("option:selected").val();
  172. var country_text = $('select[name="country_id_'+random_code+'"]').find("option:selected").text();
  173. var address = "";
  174. if(province_id != '' && province_id.indexOf("请选择") == -1){
  175. address = address + province_text;
  176. }
  177. if(city_id != '' && (area_type == 'city' || area_type == 'country') && city_id.indexOf("请选择") == -1){
  178. address = address + city_text;
  179. }
  180. if(country_id != '' && (area_type == 'country') && country_id.indexOf("请选择") == -1){
  181. address = address + country_text;
  182. }
  183. if(address == ''){
  184. address = $('input[name="hint_'+random_code+'"]').val();
  185. }
  186. $('#address_'+random_code).attr('placeholder', address);
  187. //form.render(null, 'phone_item_'+random_code);
  188. //form.render(null, 'action_li_'+random_code);
  189. $('#address_'+random_code).parent().find('.layui-select-title').find('.layui-input').attr('placeholder', address);
  190. }