遍历对table中input[type='text']的数据封装成json提交到后台处理

时间:2022-04-12 17:23:12

遍历对table中input[type='text']的数据封装成json提交到后台处理


<script>
function saveCredits(){  
        var dataJson="[";    
        var danjuid = "";  
        var name = "";  
        var specs = "";  
        var pinpai = "";  
        var unit = "";  
        var danjia = "";  
        var number = "";  
        var jine = "";  
        $("#dynamicTable tr").each(function (index, domEle){// mainTable 下的tr     
			var danjuid = ""; 
			var name = "";  
			var specs = "";  
			var pinpai = "";  
			var unit = "";  
			var danjia = "";  
			var number = "";  
			var jine = "";  
            if(index != 0){//遍历除去第一行的之外的所有input作为json数据传入后台  
                $(domEle).find("input[type='text']").each(function(index,data){
                    if(index == 0){  
                        danjuid = $(data).val();  
                    }; 
                    if(index == 1){  
                        name = $(data).val();  
                    };
                    if(index == 2){  
                        specs = $(data).val();  
                    }; 
                    if(index == 3){  
                        pinpai = $(data).val();  
                    }; 
                    if(index == 4){  
                        unit = $(data).val();  
                    };
                    if(index == 5){  
                        danjia = $(data).val();  
                    };
                    if(index == 6){  
                        number = $(data).val();  
                    };
                    if(index == 7){  
                        jine = $(data).val();  
                    };
				});
            } 
			if(name!=""|unit!=""|number!=""){
				if(parseFloat(number)>0){
				dataJson += "{"+"\"id\":\""+danjuid+"\","+"\"name\":\""+name+"\","+"\"specs\":\""+specs+"\","+"\"pinpai\":\""+pinpai+"\","+"\"unit\":\""+unit+"\","+"\"danjia\":\""+danjia+"\","+"\"number\":\""+number+"\","+"\"jine\":\""+jine+"\"},"; }
			};
        });  
        if (dataJson.lastIndexOf(",")) { 
            dataJson = dataJson.substring(0,dataJson.length -1);
			if(dataJson!=""){
				dataJson += "]";
			}else{dataJson="";}
        }
		console.log(dataJson);
		//document.getElementById('remark').value = dataJson;
		
/*    
        var testId = "${test.testId}";  
        $.ajax({  
           type: "POST",  
           url: "action",  
           data:{  
               data : dataJson,  
               testId : testId  
           },  
           success: function(result){  
               if (result.success){  
                      $.messager.alert('成功提示',result.msg,"info",function(){  
                       window.opener.doAction("****","../controller/url");//实现页面成功之后的跳转  
                       window.close();  
                      });  
                  }else{  
                      $.messager.alert('错误提示', result.msg,"error");  
                  }  
           }  
        });
*/		
		
    };  
</script>