1

我有这个将可用值存储在数组中的 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 数组传递给我的控制器,以便我可以开始在其他函数上操作数据。你能帮我解决这个问题吗?

4

1 回答 1

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
于 2013-08-05T04:20:33.410 回答