简体   繁体   English

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

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

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 (ie "1 2" 这将返回tr的innertext(即“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 混淆的应用

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() , eg: 编辑只需调用text() ,例如:

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

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());
    });
});
$(document).ready(function() { 
   var $test="";
   $('#table tr>td').click(function() { 
       $test = $(this).text(); 
       $('#input').val($test);
   )};
});

alernative: 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); 
  )};
});

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

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