我有一个如下所示的表格(简化):
<table id="product_table">
<tbody>
<tr>
<th>Product</th>
<th>Action</th>
</tr>
<tr rel="1">
<td>Product Name 1</td>
<td class="select_product">[SELECT]</td>
</tr>
<tr rel="2">
<td>Product Name 2</td>
<td class="select_product">[SELECT]</td>
</tr>
<tr rel="3">
<td>Product Name 3</td>
<td class="select_product">[SELECT]</td>
</tr>
</tbody>
</table>
我希望能够将点击事件绑定(bind)到 td/或其中的内容(尚未决定),并在调用函数“view_product”时使用父 tr 的 rel(产品 id)中包含的 id为了争论。
我尝试了多种组合,但一无所获。
有什么指点吗?
<小时/>解决方案
$('td.select_product').bind('click', function() {
var id = $(this).parent().attr("rel");
$.select_product(id);
});
放置在生成表行之后。我不确定多次绑定(bind)它是否是最好的解决方案,但它确实有效。
最佳答案
I want to be able to bind a click event to the td / or content within it (not yet decided) and use the id contained in the parent tr's rel (the product id) in a call to a function 'view_product' for arguments sake.
你的语言(或多或少)翻译成 jQuery 语法:
$("td.select_product").click(function(){
var id = $(this).parent().attr("rel");
view_product(id);
});
<小时/>
如果使用delegate()动态创建项目,请尝试绑定(bind)点击事件。 :
$("#product_table").delegate(".select_product", "click", function(){
var id = $(this).parent().attr("rel");
view_product(id);
});
关于Jquery从td中选择产品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4904412/