Javascript (Jquery) 从选定的 tr 中查找 td 的内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2401112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:08:22  来源:igfitidea点击:

(Jquery) Find the contents of a td from a selected tr

javascriptjqueryhtml

提问by Andy

I am very new to JQuery, so appologies for what may be a very simple question.

我对 JQuery 很陌生,所以对于可能是一个非常简单的问题的应用程序。

I have a table, and when i click on a row, i want the cells details to be filled into a form:

我有一个表格,当我单击一行时,我希望将单元格详细信息填写到表格中:

so simple example

这么简单的例子

<table id="table">
 <tr><td class="field1">1 </td><td class="field2">2 </td></tr>
</table>
<input id="input" type="text" />

So the JQuery:

所以JQuery:

$(document).ready(function() {
   $('#table tr').click(function() {
       $test = $(this).find('td').context.innerText) //problem here *
       $('#input').val( );
   })
  • this returns the innertext of the tr (i.e "1 2"
  • 这将返回 tr 的内部文本(即“1 2”

How am i supose to do it...

我该怎么做...

Thanks in advance

提前致谢

Andy

安迪

Edit: ok in my rush i see i messed up what i was supose to type, here is the js i am trying:

编辑:好吧,在我匆忙中,我看到我搞砸了我想输入的内容,这是我正在尝试的 js:

$(document).ready(function() {
   $('#table tr').click(function() {
       $field1 = $(this).find('td.field1').context.innerText) //problem here *
       $('#input1').val($field1);
       $field2 = $(this).find('td.field2').context.innerText) //problem here *
       $('#input12').val($field2);
   })

Appologies for the confusion

为混淆道歉

回答by karim79

If you want the text of each cell to be captured as a singe space-separated string to populate your input, you can do this:

如果您希望每个单元格的文本被捕获为单个空格分隔的字符串以填充您的输入,您可以执行以下操作:

$(document).ready(function() {
   $('#table tr').click(function() {
       var $test = $(this).find('td').map(function() {
           return $(this).text();
       }).get().join(" ");
       $('#input').val($test);
   });
});

EDIT just call text(), e.g.:

编辑只是调用text(),例如:

var $field1 = $(this).find('td.field1').text();

回答by Mark Schultheiss

$(document).ready(function() { 
   var $test="";
   $('#table tr>td').click(function() { 
       $test = $(this).text(); 
       $('#input').val($test);
   )};
});

alernative:

备选:

$(document).ready(function() { 
  var $test ="";
  $('.field1, #table').click(function() { 
      $test = $(this).text(); 
      $('#input').val($test); 
  )};
  $('.field2, #table').click(function() { 
    $test = $(this).text(); 
    $('#input').val($test); 
  )};
});

回答by user1676876

It worked for me:

它对我有用:

HTML:

HTML:

<tr class="" id="tr_id" >
    <td class="l_id">7283630222</td>
</tr>
<tr class="" id="tr_id" >
    <td class="l_id">7276684022</td>
</tr>
<input tyxp="text" id="leadID"></input>

jQuery:

jQuery:

$(document).ready(function(){
    $("tr#tr_id").click(function(){
        $("#hiddenDiv").show();
        $("#leadID").val($(this).find("td.l_id").text());
    });
});