jquery 读取页面表格中的数据 生成批量insert代码 并以ajax方式传给后台


表格的格式较为常规,首行为字段,以下各行为数据,表格的id为detailList 。


  1.     $("#submitExcel").click(function(){    //绑定单击事件  
  2.         insertStr="insert into funddetail values"// insert字符串的初始值  
  3.         $("#detailList tr:gt(0)").each(function(i){  //获取表格除首行外的所有行,并给每一行添加方法  
  4.             insertStr+="(null,";  //继续增加insert字符串  
  5.             $(this).find('td').each(function(j){ // 获取当前行中包含的所有列,并给每一列(即每一个单元格)添加方法,i为在列上的序号  
  6.                 if(j==7) //由于每一行的第7列中为一个表单控件,要用不同的方法获取取值  
  7.                 {  
  8.                     insertStr+=$(this).find('select').val()+",";  //继续增加insert字符串  
  9.                 }  
  10.                 else  
  11.                 {  
  12.                     insertStr+="'"+$(this).text()+"'," // 继续增加insert字符串,注意最后有个逗号  
  13.                 }  
  14.             })  
  15.             insertStr=insertStr.substring(0,insertStr.length-1); // 删除每一行最后一个逗号  
  16.             insertStr+="),"// 设置当前行的insert语句结束,加上逗号,以便于接下一行数据  
  17.         })  
  18.         insertStr=insertStr.substring(0,insertStr.length-1); // 删除最终insert字符串最后的那个逗号  
  19.   
  20. // 以ajax方式将上述得到的insert字符串传递给后台页面进行处理  
  21.  $.ajax({  
  22.             type:"post",  
  23.             data:{insertstring:insertStr},  
  24.             dataType:'html',  
  25.             url:"phpCode/saveExcel.php",  
  26.             success:function(data){  
  27.                 alert(data);  
  28.             }  
  29.         });  
  30.     }); 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值