简体   繁体   English

jQuery在tr中获取子文本

[英]jQuery get children text in tr

Here is the html structure 这是html结构

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table class="table"> <thead> <tr> <th>NAME</th> <th>DES</th> </tr> </thead> <tbody id="sub_mission_list"> <tr> <td>Q1</td> <td> <textarea id="mission_description" name="mission_description">T1</textarea> </td> </tr> <tr> <td>Q2</td> <td> <textarea id="mission_description" name="mission_description">T2</textarea> </td> </tr> </tbody> </table> 

I want to get the value in <textarea> 我想在<textarea>获取值

I did and here is my solution, but I don't think it's a good way to do it. 我做了,这是我的解决方案,但我不认为这是一个很好的方法。 What's the best way to achieve that? 实现这一目标的最佳方法是什么?

_.each($('#sub_mission_list tr'), function(value) {
  var describe = $($($(value).children()[1]).children()[0]).val();
  console.log(describe);
});

Use jQuery.each() for iteration, and use .find() to target textarea 使用jQuery.each()进行迭代,并使用.find()来定位textarea

$('#sub_mission_list tr').each(function() {
  var describe = $(this).find('textarea').val();
  console.log(describe);
});

 $('#sub_mission_list tr').each(function() { var describe = $(this).find('textarea').val(); console.log(describe); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <body> <table class="table"> <thead> <tr> <th>NAME</th> <th>DES</th> </tr> </thead> <tbody id="sub_mission_list"> <tr> <td>Q1</td> <td> <textarea id="mission_description" name="mission_description">T1</textarea> </td> </tr> <tr> <td>Q2</td> <td> <textarea id="mission_description" name="mission_description">T2</textarea> </td> </tr> </tbody> </table> </body> 

Note: Identifiers in HTML must be unique. 注意:HTML中的标识符必须是唯一的。

You can iterate over the textarea by updating the selector and use each() method to iterate over the jQuery collection. 您可以通过更新选择器迭代textarea并使用each()方法迭代jQuery集合。

// To get the textarea within the second td, update selector
// to more specific `$('#sub_mission_list tr td:nth-child(2) textarea')`
$('#sub_mission_list tr td textarea').each(function() {
  var describe = this.value; // get the text value
  console.log(describe);
});

 // To get the textarea within the second td, update selector // to more specific `$('#sub_mission_list tr td:nth-child(2) textarea')` $('#sub_mission_list tr td textarea').each(function() { var describe = this.value; // get the text value console.log(describe); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <body> <table class="table"> <thead> <tr> <th>NAME</th> <th>DES</th> </tr> </thead> <tbody id="sub_mission_list"> <tr> <td>Q1</td> <td> <textarea id="mission_description" name="mission_description">T1</textarea> </td> </tr> <tr> <td>Q2</td> <td> <textarea id="mission_description" name="mission_description">T2</textarea> </td> </tr> </tbody> </table> </body> 


In case you want to get the result as an array then use map() method along with get() method. 如果您想将结果作为数组获取,则使用map()方法和get()方法。

var res = $('#sub_mission_list tr td textarea').map(function() {
  return this.value; // get the text value
}).get();

 var res = $('#sub_mission_list tr td textarea').map(function() { return this.value; // get the text value }).get(); console.log(res); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <body> <table class="table"> <thead> <tr> <th>NAME</th> <th>DES</th> </tr> </thead> <tbody id="sub_mission_list"> <tr> <td>Q1</td> <td> <textarea id="mission_description" name="mission_description">T1</textarea> </td> </tr> <tr> <td>Q2</td> <td> <textarea id="mission_description" name="mission_description">T2</textarea> </td> </tr> </tbody> </table> </body> 

使用文本区域直接$('#sub_mission_list tr td textarea').each(function() {

Use input type selector. 使用输入类型选择器。

 $.each($('#sub_mission_list textarea'), function(value) { var describe =$(this).val(); console.log(describe); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script> <table class="table"> <thead> <tr> <th>NAME</th> <th>DES</th> </tr> </thead> <tbody id="sub_mission_list"> <tr> <td>Q1</td> <td> <textarea id="mission_description" name="mission_description">T1</textarea> </td> </tr> <tr> <td>Q2</td> <td> <textarea id="mission_description" name="mission_description">T2</textarea> </td> </tr> </tbody> </table> 

also you can search in table using .find of jquery 你也可以使用.find的jquery在表中搜索

    var getValues = $('#sub_mission_list').find('textarea');

    getValues.each(function(){
        console.log($( this ).val());
    });

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

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