简体   繁体   English

使用Jquery获取HTML表数据

[英]Getting HTML table data using Jquery

I am a newbie in Jquery. 我是Jquery的新手。 First of all I would like to say that I have researched almost a day regarding this query. 首先,我想说我已经花了将近一天的时间对此查询进行了研究。 TRied numberous methods, but none that works. 尝试了许多方法,但没有一种有效。 I am trying to code a billing module with HTML, PHP and Jquery. 我正在尝试使用HTML,PHP和Jquery编写一个计费模块。 Until now, I have successfully created the HTML tables. 到目前为止,我已经成功创建了HTML表。 I would like the data from these tables to the retrieved to a PHP page, so that I can print them with a proper formatting. 我希望将这些表中的数据检索到PHP页面,以便可以使用正确的格式打印它们。

The HTML Table is as follows. HTML表如下。

<table id="items">
    <tbody>

      <tr>
          <th>SlNo</th>
          <th>Item</th>
          <th>Unit Cost</th>
          <th>Quantity</th>
          <th>Price</th>
      </tr>

 <tr class="item-row">
         <td class="item-name">
           <div class="delete-wpr">
          <input type="text" class="slno"/>
           <a class="delete" href="javascript:;" title="Remove row">X</a>
           </div>
         </td>

           <td><input type="text" class="slno"/></td>           

           <td><input type="text" class="cost"/></td>
           <td><input type="text" class="qty"/></td>
           <td><span class="price"></span></td>
      </tr>  



    <input type="button" value="submit" onClick="storeAndShowTableValues()"/>


    <tbody>

Now, the Jquery is as follows. 现在,Jquery如下。

     var TableData = new Array();
     $('#items tr').each(function(row, tr){

     TableData[row]={
        "ItemNum" : $(tr).find('td:eq(0)').text(), 
        "Itemname" :$(tr).find('td:eq(1)').text(),
         "unitprice" : $(tr).find('td:eq(2)').text(),
         "Qty" : $(tr).find('td:eq(3)').text(),
        "price" : $(tr).find('td:eq(4)').text()
    }
}
); 
TableData.shift();  // first row is the table header - so remove

var TableData = JSON.stringify(TableData);

But the JSON object I am getting is garbage. 但是我得到的JSON对象是垃圾。 The table data is not retrieved. 不检索表数据。 Please help. 请帮忙。

To get the value of an input field in jQuery you need to use the .val() function. 要获取jQuery中输入字段的值,您需要使用.val()函数。

Also make sure that you select the input element within your td tag 还要确保您在td标签中选择了input元素

http://api.jquery.com/val/ http://api.jquery.com/val/

So your code should look something like this: 因此,您的代码应如下所示:

var TableData = new Array();

    $('#items tr').each(function(row, tr){

    TableData[row]={
        "ItemNum" : $(tr).find('td:eq(0) input').val(),
        "Itemname" :$(tr).find('td:eq(1) input').val(),
         "unitprice" : $(tr).find('td:eq(2) input').val(),
         "Qty" : $(tr).find('td:eq(3) input').val(),
        "price" : $(tr).find('td:eq(4)').text()
    }
}
);
TableData.shift();  // first row is the table header - so remove

TableData = JSON.stringify(TableData);
var TableData = new Array();

$('#items tr').each(function(row){

TableData[row]={
     "ItemNum" : $(this).find('td:eq(0) input').val(), 
    "Itemname" :$(this).find('td:eq(1) input').val(),
     "unitprice" : $(this).find('td:eq(2) input').val(),
     "Qty" : $(this).find('td:eq(3) input').val(),
    "price" : $(this).find('td:eq(4) input').val(),
  }
  }
); 
 TableData.shift();  // first row is the table header - so remove

  var TableData = JSON.stringify(TableData);

不要花很多时间,但是我认为您需要获取val()而不是text()或html()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM