【问题标题】:How to get a specific tr in an html page?如何在 html 页面中获取特定的 tr?
【发布时间】:2016-05-24 10:29:09
【问题描述】:

我试图在我的 ui 中显示下拉列表的选定值。

<tr class="odd" rowid="1" height="50">          
        <td>
          <select disabled name="item[]" id="item_1" class="dropdown required" tabindex="18">
        <?php echo $itemOption;?>
        </select>
        </td>
        <td >
          <a href="javascript:void(0);"  style="height:40px; width:45px"  class="button add_new_row ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only ui-state-focus" data-role="button" ><span class="ui-button-text" style="padding:0;" disabled>+</span></a>                            
        </td>           
</tr>

在 jquery 中,我需要获取这个 tr 来复制我的下拉列表。这是我的jQuery

 var thisRow = $(this).closest('tr');   
 newid=parseInt(thisRow.attr('rowid'))+1;
 var count = $('.dataTable input.timepicker').length + 1;
 newRow = thisRow.clone(true).insertAfter(thisRow).find('input').val('').end().

但是我在 thisRow 中没有定义。可能是什么原因。有人请纠正我。

【问题讨论】:

  • this 指的是什么?
  • 我正在尝试获取当前对象。这意味着当前页面。不知道是不是写的。
  • @django 但是什么返回console.log(this);???您需要提供有关您发布的代码的更多上下文,因为没有人知道this 指的是什么……也许您只需要var thisRow = $('tr[rowid=1]);。但还是要注意,因为这个TR有ID设置的后代(item_1),在DOM中添加克隆会导致无效的HTML标记,得到一些具有相同ID的多个元素

标签: javascript jquery html tags


【解决方案1】:

您应该使用任何操作调用此脚本,例如在函数中绑定或在任何 event 上使用 trigger 等。

你想把它叫到哪里??

举个例子,假设我有一个类似的 HTML

<div id="div1">
    <span class="test">Call Closest Division</span>
</div>
<div id="div2">
    <span class="test">Call Closest Division</span>
</div>

我想在点击span 时得到closest 分区。我的脚本是这样的

$('.test').click(function(){
  var id = $(this).closest('div').attr('id');
  alert(id);
})

别忘了包含jquery 库。

Working demo

【讨论】:

  • 很好的解释。现在工作正常。我错过的一些基础知识是从你的评论中得到的。非常感谢
  • 那为什么你不应该投票并标记为答案呢?这将对其他用户有所帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-23
  • 2011-09-30
  • 1970-01-01
  • 1970-01-01
  • 2011-07-03
相关资源
最近更新 更多