这是我的Html代码,我只想选择选中的下一个元素复选框
<tr class="" role="row">
<td class="e-rowcell e-templatecell" role="gridcell" style="text-align: center;">
<input type="checkbox" class="XAxisrowCheckbox">
</td>
<td class="e-rowcell e-hide" role="gridcell" style="text-align: left;">0</td>
<td class="e-rowcell" role="gridcell" style="text-align: left;">Location ID</td>
</tr>
<tr class="e-alt_row" role="row" aria-selected="true">
<td class="e-rowcell e-templatecell e-selectionbackground e-active" role="gridcell" style="text-align: center;">
<input type="checkbox" class="XAxisrowCheckbox">
</td>
<td class="e-rowcell e-hide e-selectionbackground e-active" role="gridcell" style="text-align: left;">1</td>
<td class="e-rowcell e-selectionbackground e-active" role="gridcell" style="text-align: left;">Location Name</td>
</tr>jQuery码
$(document).on('click', '.XAxisrowCheckbox', function () {
$(".XAxisrowCheckbox").parent().next(this).css('background-color','red');
});但问题是,它正在将背景颜色应用于所有td,而不管所选的是什么。
我得到的是

我期望的是只更改这个被选中的元素。

发布于 2015-08-18 09:14:48
为此最好使用最近的:
$(document).on('click', '.XAxisrowCheckbox', function () {
$(this).closest('td').css('background-color','red');
});它读起来比parent和next好得多,如果需要的话,您也可以在find语句上链接。例如:
$(this).closest('td').find('a').css('color','red');这实际上是说“在dom树上工作,直到找到第一个包装单击元素的td,然后再向下走,直到找到所有的a元素”。
编辑:实际代码,为我解决后的工作
$(this).closest('td').next().css('background-color','red');发布于 2015-08-18 09:14:11
用这个代替
$(document).on('click', '.XAxisrowCheckbox', function () {
$(this).parent().next().css('background-color','red');
});发布于 2015-08-18 09:14:36
删除下一个函数中的此参数,并将其放入主选择器中:
$(document).on('click', '.XAxisrowCheckbox', function () {
$(this).parent().next().css('background-color','red');
});https://stackoverflow.com/questions/32068182
复制相似问题