我有这个javascript,可以在数组中存储能力值:
<script>
$(function(){
$('#preview').click(function(){
var thesum=0;
var rowid=[];
var rowfields=[];
var supplier = document.getElementById("sid").value;
var terms=document.getElementById("terms").value;
var podate=document.getElementById("date1").value;
var reqdate=document.getElementById("date2").value;
var contp=document.getElementById("contactp").value;
var count = ($('#listOfProducts tr').length);
//loop start
var i=0;
grid=$('#listOfProducts input[type="checkbox"]:checked').each(function(){
var $row = $(this).parents('tr');
var $trid =$(this).closest('tr').attr('id');
rowid[i]=$trid;
rowfields.push({itemname: $row.find('td:eq(0)').text(), productname:$row.find('td:eq(1)').text(), productdesc: $row.find('td:eq(2)').text(), unitprice:$row.find('td:eq(3)').text(), quantity:$row.find('td:eq(5) input').val(), amount:$row.find('td:eq(6) input').val()});
i++;
});//each close
var dataString = JSON.stringify(rowfields);
});//preview click close
});//function close
</script>
我需要将rowfields数组传递给我的控制器,这样我才能开始操作其他函数的数据。你能帮我解决这个问题吗?
答案 0 :(得分:1)
Jquery ajax
$(document).ready(function(){
$('#preview').click(function(){
var thesum=0;
var rowid=[];
var rowfields=[];
var supplier = document.getElementById("sid").value;
var terms=document.getElementById("terms").value;
var podate=document.getElementById("date1").value;
var reqdate=document.getElementById("date2").value;
var contp=document.getElementById("contactp").value;
var count = ($('#listOfProducts tr').length);
//loop start
var i=0;
grid=$('#listOfProducts input[type="checkbox"]:checked').each(function(){
var $row = $(this).parents('tr');
var $trid =$(this).closest('tr').attr('id');
rowid[i]=$trid;
rowfields.push({itemname: $row.find('td:eq(0)').text(), productname:$row.find('td:eq(1)').text(), productdesc: $row.find('td:eq(2)').text(), unitprice:$row.find('td:eq(3)').text(), quantity:$row.find('td:eq(5) input').val(), amount:$row.find('td:eq(6) input').val()});
i++;
});//each close
var dataString = JSON.stringify(rowfields);
//ajax function
//http://api.jquery.com/jQuery.ajax/
$.ajax({
//controller path
url: "/path/to/url",
type: "POST",
dataType: "json",
contentType: "json",
data: dataString
success: function(response){
console.log(response);
},
error: function(response){
console.log(response);
}
}); //ajax close
});//preview click close
});//function close