封装提交数据向后台传的参数

时间:2022-08-26 10:05:05
  •  <ul>
                                    <li>
                                        <p>现居住地址:</p>
                                        <input name="address" id="address" class="input u10_inputlong" type="text" value="">
                                    </li>
                                    <li>
                                        <p>手机号码:</p>
                                        <input onkeyup="value=this.value.replace(/\D+/g,'');" name="tel" id="tel" maxlength="11" class="input u10_tex2" type="text" value="">
                                    </li>
                                    <li>
                                        <p>第一联系人:</p>
                                        <input name="contact1" id="contact1" class="input u10_tex2" type="text" value="">
                                    </li>
                                    <li>
                                        <p>关系:</p>
                                        <input type="radio" name="contact1_re" value="家庭成员" id="contact1_re_0">
                                        <label for="contact1_re_0">家庭成员</label>    
                                        <input type="radio" name="contact1_re" value="朋友" id="contact1_re_1">
                                        <label for="contact1_re_1">朋友</label>    
                                        <input type="radio" name="contact1_re" value="商业伙伴" id="contact1_re_2">
                                        <label for="contact1_re_2">商业伙伴</label>    </li>
                                    <li>
                                        <p>手机号码:</p>
                                        <input onkeyup="value=this.value.replace(/\D+/g,'');" name="contact1_tel" maxlength="11" id="contact1_tel" class="input u10_tex2" type="text" value="">
                                    </li>
                                    <li>
                                        <p>其他:</p>
                                        <input name="contact1_other" id="contact1_other" class="input u10_inputlong" type="text" value="">
                                    </li>
                                    <li>
                                        <p>第二联系人:</p>
                                        <input name="contact2" id="contact2" class="input u10_tex2" type="text" value="">
                                    </li>
                                    <li>
                                        <p>关系:</p>
                                        <input type="radio" name="contact2_re" value="家庭成员" id="contact2_re_0">
                                        <label for="contact2_re_0">家庭成员</label>    
                                        <input type="radio" name="contact2_re" value="朋友" id="contact2_re_1">
                                        <label for="contact2_re_1">朋友</label>    
                                        <input type="radio" name="contact2_re" value="商业伙伴" id="contact2_re_2">
                                        <label for="contact2_re_2">商业伙伴</label>    </li>
                                    <li>
                                        <p>手机号码:</p>
                                        <input onkeyup="value=this.value.replace(/\D+/g,'');" name="contact2_tel" maxlength="11" id="contact2_tel" class="input u10_tex2" type="text" value="">
                                    </li>
                                    <li>
                                        <p>其他:</p>
                                        <input name="contact2_other" id="contact2_other" class="input u10_inputlong" type="text" value="">
                                    </li>
                                    <li>
                                        <p>第三联系人:</p>
                                        <input name="contact3" id="contact3" class="input u10_tex2" type="text" value="">
                                    </li>
                                    <li>
                                        <p>关系:</p>
                                        <input type="radio" name="contact3_re" value="家庭成员" id="contact3_re_0">
                                        <label for="contact3_re_0">家庭成员</label>    
                                        <input type="radio" name="contact3_re" value="朋友" id="contact3_re_1">
                                        <label for="contact3_re_1">朋友</label>    
                                        <input type="radio" name="contact3_re" value="商业伙伴" id="contact3_re_2">
                                        <label for="contact3_re_2">商业伙伴</label>    </li>
                                    <li>
                                        <p>手机号码:</p>
                                        <input onkeyup="value=this.value.replace(/\D+/g,'');" name="contact3_tel" maxlength="11" id="contact3_tel" class="input u10_tex2" type="text" value="">
                                    </li>
                                    <li>
                                        <p>其他:</p>
                                        <input name="contact3_other" id="contact3_other" class="input u10_inputlong" type="text" value="">
                                    </li>
                                </ul>
                                <input type="button" class="u10_but" value="保存/下一步" onclick="editContact()">
    

      //提交表单js方法, 主要看封装的makevar()方法

    function editContact(){
        p = makevar(['contact2_other','contact2_tel','contact1_tel','contact1_other','contact2_re','contact1_re','contact1','contact2','tel','address','contact3_tel','contact3','contact3_other','contact3_re']);
    
    	$.ajax({
    		url: "/member/memberinfo/editcontact",
    		data: p,
    		timeout: 5000,
    		cache: false,
    		type: "post",
    		dataType: "json",
    		success: function (d, s, r) {
    				if(d.status==1){
    					layer.msg('保存成功');	
    				}else  layer.msg(d.message);
    			
    		}
    	});
    }
    
    
    
    function makevar(v){
    	var d={};
    	for(i in v){
    		var id = v[i];
    		d[id] = $("#"+id).val();
    		if(!d[id]) d[id] = $("input[name='"+id+"']:checked").val();
    	}
    	return d;
    }
    

      //获取数据填入dom

function getEditContact () {
    layer.msg('加载中......', {
        icon: 16,
        shade: 0.01
    });
   $.get('/member/memberinfo/editcontact',function(res){
      if(res.data){
            for(var key in res.data){ 
                $('#'+key).val(res.data[key]);  
                if(!$('#'+key).length){   //如果没有这个元素, 则循环name为key的input,val与key所对应的值相等时,则让该元素选中
                   $.each($("input[name="+key+"]"),function(i,k){
                      if ($(this).val() == res.data[key]){
                        $(this).prop('checked',true);
                      }
                   })
                   
                }
            } 
        } 

   },'json')
}