最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Getting textbox values inside table using jQuery - Stack Overflow

programmeradmin0浏览0评论

Html Table

<table id="tb">
<tr>
  <th>First Name</th>
  <th>Last Name </th>
  <th>Coutry</th>
</tr>

<tr>
  <td><input type="text" id="FName"/> </td>
  <td><input type="text" id="LName"/> </td>
   <td><input type="text" id="Country"/> </td>
</tr>

<tr>
  <td><input type="text" id="FName"/> </td>
  <td><input type="text" id="LName"/> </td>
   <td><input type="text" id="Country"/> </td>
</tr>
</table>

Jquery

//Here I am looping through all the rows in table
$('#tb tr').each(function(i, row) {
  //Here I need to loop the tr again ( i.e. row) 
  $(row, "input").each(function(i, sr) {
    //here  I want to get all the textbox values
    alert($(sr).eq(0).val());
    alert($(sr).eq(1).val());
    alert($(sr).eq(2).val());
  });
});

But I am getting undefined values.

I want to get all the values from text boxes which are inside the table.

Please help me resolve the issue.

Html Table

<table id="tb">
<tr>
  <th>First Name</th>
  <th>Last Name </th>
  <th>Coutry</th>
</tr>

<tr>
  <td><input type="text" id="FName"/> </td>
  <td><input type="text" id="LName"/> </td>
   <td><input type="text" id="Country"/> </td>
</tr>

<tr>
  <td><input type="text" id="FName"/> </td>
  <td><input type="text" id="LName"/> </td>
   <td><input type="text" id="Country"/> </td>
</tr>
</table>

Jquery

//Here I am looping through all the rows in table
$('#tb tr').each(function(i, row) {
  //Here I need to loop the tr again ( i.e. row) 
  $(row, "input").each(function(i, sr) {
    //here  I want to get all the textbox values
    alert($(sr).eq(0).val());
    alert($(sr).eq(1).val());
    alert($(sr).eq(2).val());
  });
});

But I am getting undefined values.

I want to get all the values from text boxes which are inside the table.

Please help me resolve the issue.

Share Improve this question edited Apr 15, 2016 at 13:37 Rino Raj 6,2642 gold badges28 silver badges44 bronze badges asked Apr 15, 2016 at 13:32 PearlPearl 9,4558 gold badges45 silver badges60 bronze badges 2
  • 1 IDs must be unique. Also, when/how do you execute your jQuery? – j08691 Commented Apr 15, 2016 at 13:34
  • In button click event. I don't have control over ids as they are generated dynamically – Pearl Commented Apr 15, 2016 at 13:35
Add a ment  | 

3 Answers 3

Reset to default 3

You can use

$('#tb tr').each(function(i, row) {
   $(this).find('input').each(function() {
     alert($(this).val())
   })
});

Working Demo

$('#tb tr').each(function(i, row) {
   $(this).find('input').each(function() {
     alert($(this).val())
   })
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="tb">
  <tr>
    <th>First Name</th>
    <th>Last Name</th>
    <th>Coutry</th>
  </tr>

  <tr>
    <td>
      <input type="text" id="FName" value="1" />
    </td>
    <td>
      <input type="text" id="LName" value="2" />
    </td>
    <td>
      <input type="text" id="Country" value="3" />
    </td>
  </tr>

  <tr>
    <td>
      <input type="text" id="FName" value="4" />
    </td>
    <td>
      <input type="text" id="LName" value="5" />
    </td>
    <td>
      <input type="text" id="Country" value="6" />
    </td>
  </tr>
</table>

You can just use :

$('#tb tr input[type=text]').each(function(){

  alert($(this).val());

});

The line has the context switched with the selector

$(row, "input").each(function (i, sr) {

It should be

$("input", row).each(function (i, sr) {

or

$(row).find("input").each(function (i, sr) {
发布评论

评论列表(0)

  1. 暂无评论