简体   繁体   中英

Getting HTML table data using Jquery

I am a newbie in 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. Until now, I have successfully created the HTML tables. 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.

The HTML Table is as follows.

<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.

     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. 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.

Also make sure that you select the input element within your td tag

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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