简体   繁体   English

从表TR ID和TD ID获取价值

[英]Getting Value From Table TR id and TD id

I have problems getting id from tr and td in my table. 我在从表中的tr和td获取ID时遇到问题。

Let's say I have a table like this: 假设我有一个这样的表:

<table class="table table-hover" id="table_tingkat_jual">
<thead>
 <tr>
   <th>Tingkat Penjualan</th>
   <th>SA</th>
   <th>Kode SA</th>
   <th>Kuantiti Terendah (lusin)</th>
   <th>Kuantiti Tertinggi (lusin)</th>
</tr>
</thead>
<tbody>
 <tr id='0'>
  <td>Diatas Rata-Rata</td>
  <td id='1'>1 </td>
  <td>AG</td>
  <td>3870</td>
  <td>5782</td>
 </tr>
 <tr id='0'>
  <td>Diatas Rata-Rata</td>
  <td id='3'>3 </td>
  <td>CA</td>
  <td>1080</td>
  <td>3780</td>
 </tr>
</tbody>
</table>

I want to getting id from TR and id FROM td for each tr clicked in specific table (table_tingkat_jual). 我想从特定表(table_tingkat_jual)中单击的每个tr分别从TR和ID FROM td获取ID。

This is my syntax in jQuery: 这是我在jQuery中的语法:

$('#table_tingkat_jual tr').click(function(){
    this.stopPropagation();
});

$('#table_tingkat_jual tr').click(function() {
    var trid = $(this).closest('tr').attr('id');
    alert("TR ID " + trid);
    var tdid = $(this).closest('td').attr('id');
    alert("TD ID " + tdid);
});

But when I clicked the row in that table, nothing happened. 但是,当我单击该表中的行时,没有任何反应。 What I want is alert me the id. 我想要的是通知我ID。 (See the alert function). (请参阅警报功能)。

What's wrong? 怎么了?

Update from chat: 通过聊天更新:

It turns out the problem is that the table is loaded dynamically via ajax, so a delegated event is needed (in addition to the other fixes): 事实证明,问题在于该表是通过ajax动态加载的,因此需要一个委托事件(除了其他修复程序之外):

$(document).on('click', '#table_tingkat_jual tr', function (e) {
    e.stopPropagation();
    var $this = $(this);
    var trid = $this.closest('tr').data('id');
    alert("TR ID " + trid);
    var tdid = $this.find('td[data-id]').data('id');
    alert("TD ID " + tdid);
});

Previous details: 以前的详细信息:

There are several issues, not the least of which is the use of duplicate ID's in the HTML (which is invalid). 有几个问题,其中最重要的是在HTML中使用重复ID(无效)。

You also do not need separate, identical, selectors to handle stopPropogation (assuming you actually need stopPropogation at all (eg to avoid clicks in parent objects). 您也不需要单独的,相同的选择器来处理stopPropogation (假设您实际上确实需要stopPropogation (例如,避免单击父对象)。

It appears you also want to drill down for the TD values, so try this: 看来您也想深入了解TD值,因此请尝试以下操作:

JSFiddle: http://jsfiddle.net/TrueBlueAussie/fw5ty/ JSFiddle: http : //jsfiddle.net/TrueBlueAussie/fw5ty/

$('#table_tingkat_jual tr').click(function(e) {
    e.stopPropagation();
    var $this = $(this);
    var trid = $this.closest('tr').data('id');
    alert("TR ID " + trid);
    var tdid = $this.find('td[data-id]').data('id');
    alert("TD ID " + tdid);
});

data('id') is a short-cut for attr('data-id') . data('id')attr('data-id')的捷径。

note I have changed your HTML to use data-id attributes instead of id= so that duplicate values are allowable. 请注意,我已将HTML更改为使用data-id属性而不是id=以便允许重复值。

<table class="table table-hover" id="table_tingkat_jual">
    <thead>
        <tr>
            <th>Tingkat Penjualan</th>
            <th>SA</th>
            <th>Kode SA</th>
            <th>Kuantiti Terendah (lusin)</th>
            <th>Kuantiti Tertinggi (lusin)</th>
        </tr>
    </thead>
    <tbody>
        <tr data-id='0'>
            <td>Diatas Rata-Rata</td>
            <td data-id='1'>1</td>
            <td>AG</td>
            <td>3870</td>
            <td>5782</td>
        </tr>
        <tr data-id='0'>
            <td>Diatas Rata-Rata</td>
            <td data-id='3'>3</td>
            <td>CA</td>
            <td>1080</td>
            <td>3780</td>
        </tr>
    </tbody>
</table>

If you really must use duplicate ID's (which I strongly recommend you fix) use this code instead: 如果您确实必须使用重复的ID(强烈建议您修复),请改用以下代码:

http://jsfiddle.net/TrueBlueAussie/fw5ty/1/ http://jsfiddle.net/TrueBlueAussie/fw5ty/1/

$('#table_tingkat_jual tr').click(function(e) {
    e.stopPropagation();
    var $this = $(this);
    var trid = $this.closest('tr').attr('id');
    alert("TR ID " + trid);
    var tdid = $this.find('td[id]').attr('id');
    alert("TD ID " + tdid);
});

you have two elements with the same id: 您有两个具有相同ID的元素:

<tr id='0'>

id should be unique. id应该是唯一的。 Use a class if you want both to be 0, or assign one a different value. 如果您希望两者都为0,请使用一个类,或为一个分配不同的值。

The issue is that .closest starts looking from the target element and up, not down on the DOM tree, and since your event is on tr it never gets to the td , that's why you always get undefined, if what you want is to get the id of the first td with one, try using .find() : 问题是.closest开始从目标元素开始向上看,而不是从DOM树上向下看,并且由于您的事件在tr所以它永远不会到达td ,这就是为什么如果要获取的内容总是不确定的原因第一个tdid和一个,请尝试使用.find()

$('#table_tingkat_jual tr').click(function() {
    var trid = $(this).closest('tr').attr('id');
    alert("TR ID " + trid);
    var tdid = $(this).find('td[id]').attr('id');
    alert("TD ID " + tdid);
});

Sample fiddle 样品小提琴

Also I'd get rid of: 我也会摆脱:

$('#table_tingkat_jual tr').click(function(){
    this.stopPropagation();
});

And finally, you're not supposed to have repeated id attributes on html, so you should change those or use a data attribute or a class instead. 最后,您不应该在html上具有重复的id属性,因此您应该更改它们或使用data属性或类。

Maybe you forgot to include jquery? 也许您忘记了包含jquery? I just included jQuery from google and let the script wait until the document is completly loaded. 我只包含了Google的jQuery,并让脚本等待文档完全加载完毕。

I also gave the different ids, but that was not the problem i think. 我还提供了不同的ID,但这不是我认为的问题。

I also recommend giving all the an ID, because now he alerts undefined ID. 我还建议给所有ID一个ID,因为现在他会提醒未定义的ID。

For me it works like this: 对我来说,它是这样的:

<html>
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    $(document).ready(function(){
    $('#table_tingkat_jual tr').click(function() {
    var trid = $(this).closest('tr').attr('id');
    alert("TR ID " + trid);
    var tdid = $(this).closest('td').attr('id');
    alert("TD ID " + tdid);
    });
    });
</script>
</head>
<body>
<table class="table table-hover" id="table_tingkat_jual">
<thead>
 <tr id='0'>
   <th>Tingkat Penjualan</th>
   <th>SA</th>
   <th>Kode SA</th>
   <th>Kuantiti Terendah (lusin)</th>
   <th>Kuantiti Tertinggi (lusin)</th>
</tr>
</thead>
<tbody>
 <tr id='1'>
  <td>Diatas Rata-Rata</td>
  <td id='1'>1 </td>
  <td>AG</td>
  <td>3870</td>
  <td>5782</td>
 </tr>
 <tr id='2'>
  <td>Diatas Rata-Rata</td>
  <td id='3'>3 </td>
  <td>CA</td>
  <td>1080</td>
  <td>3780</td>
 </tr>
</tbody>
</table>
<body>
</html>

Hope it helps. 希望能帮助到你。

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

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