我动态地生成了这个表
<table>
<tbody>
<tr>
<th>Email</th>
<th>Name</th>
<th>Date</th>
<th>Action</th>
</tr>
<tr id="address-row-2">
<td><input type="text" value="com@com.com" id="email-1" class="email"></td>
<td><input type="text" value="John Doe 1" id="name-1" class="name"></td>
<td>1 September 2013</td>
<td><a href="#" class="orange" rel="edit-2">submit</a></td>
</tr>
<tr id="address-row-3">
<td><input type="text" value="com@com.com" id="email-2" class="email"></td>
<td><input type="text" value="John Doe 2" id="name-2" class="name"></td>
<td>2 September 2013</td>
<td><a href="#" class="orange" rel="edit-3>submit</a></td>
</tr>
<tr id="address-row-4">
<td><input type="text" value="com@com.com" id="email-3" class="email"></td>
<td><input type="text" value="John Doe 3" id="name-3" class="name"></td>
<td>3 September 2013</td>
<td><a href="#" class="orange" rel="edit-4>submit</a></td>
</tr>
</tbody>
</table>submit按钮用于使输入可编辑,以更改内容内联。
$("input.email:text, input.name:text").attr("disabled",true);
$(".orange").attr('href', 'javascript:void(0)').click(function (){
$("input.email:text, input.name:text").addClass("show-border");
$("input.email:text, input.name:text").removeAttr("disabled");
});如何将按钮的onclick事件指向来自其行id的输入。更确切地说,如果我在第三行单击提交按钮,我希望只编辑来自第三行的输入。
发布于 2013-09-16 13:23:11
您需要遍历DOM以找到相对于单击按钮的元素。试试这个:
$(".orange").attr('href', 'javascript:void(0)').click(function (){
var $row = $(this).closest('tr');
$("input.email, input.name", $row).addClass("show-border");
$("input.email, input.name", $row).removeAttr("disabled");
});Example fiddle
发布于 2013-09-16 13:22:27
只需使用closest在DOM中搜索:
$('input').click(function(){
var rowId = $(this).closest('tr').attr('id');
});生活演示:http://jsfiddle.net/6uMjR/
发布于 2013-09-16 13:23:10
你可以:
$(".orange").click(function() {
$(this).siblings("input:text").prop("disabled", false);
});https://stackoverflow.com/questions/18829060
复制相似问题