使用 jQuery 1.6.1
当复选框被选中或取消选中时,我想更改行的背景颜色。我最接近的 tr 没有在这里被选中。我有一个工作代码,我正在更改为插件,这个问题只发生在插件中。
称呼:
$(".tableHighlightSelector tr td input[type='checkbox']").checkBoxTableHighlighter();
插件代码:
//checkbox row highlighter
jQuery.fn.checkBoxTableHighlighter = function(options){
var defaults = {
highlightClass: "rowhighlight"
};
var options = $.extend(defaults, options);
var highlight = {
RemoveHighlightForAllNotChecked: function ($item) {
$item.filter(':not(:checked)').closest("tr").removeClass(defaults.highlightClass);
},
CheckBoxRowSelect: function (e, $clicked) {
if ($clicked.attr("checked") == true) {
$clicked.closest("tr").addClass(defaults.highlightClass);
highlight.RemoveHighlightForAllNotChecked($('input[name=' + $clicked.attr("name") + ']')); //when we make checkbox behave like a radio button
}
else
$clicked.closest("tr").removeClass(defaults.highlightClass);
}
}
return this.each(function() {
var $obj = $(this);
var jsObj = this;
//onload
highlight.CheckBoxRowSelect('', $obj);
//click
$obj.click(function(){
highlight.CheckBoxRowSelect(e, $(this));
});
//keyup
$obj.keyup(function(){
highlight.CheckBoxRowSelect(e, $(this));
});
});
};
HTML:
<table width="300px" class="tableHighlightSelector">
<tr>
<td colspan="2">Group 2</td>
</tr>
<tr>
<td width="20px"><input type="checkbox" name="group2" value="a" checked="checked" /></td>
<td>A</td>
</tr>
<tr>
<td width="20px"><input type="checkbox" name="group2" value="b" /></td>
<td>B</td>
</tr>
<tr>
<td width="20px"><input type="checkbox" name="group2" value="c" /></td>
<td>C</td>
</tr>
<tr>
<td width="20px"><input type="checkbox" name="group2" value="d" /></td>
<td>D</td>
</tr>
</table>