Javascript 查找其他复选框
请参阅js代码中的coments 我有两个复选框Javascript 查找其他复选框,javascript,jquery,Javascript,Jquery,请参阅js代码中的coments 我有两个复选框 <input type="checkbox" name="first" id="first" /> <input type="checkbox" name="second" id="second" /> 你可以做: $('input[type=checkbox]').click(function() { /* HERE i want to manipulate the checkbox which is not
<input type="checkbox" name="first" id="first" />
<input type="checkbox" name="second" id="second" />
你可以做:
$('input[type=checkbox]').click(function() {
/*
HERE i want to manipulate the checkbox which is not clicked
NOTICE: they are only two checkboxes in page
I know that I can do something like this
if($(this).attr('id') == "first) {
do something
} else {
etc...
}
But I think that way is against DNRY
Can you recommend me some more elegenat solution ?
*/
//you can get the other checkbox by filtering out the element you clicked
//in this case i hide it
$('input:checkbox').not(this).hide();
});
您只想使用
.not
过滤掉当前单击的项目(此
)
您可以使用.not()
功能排除当前/单击的复选框。例如
$(function() {
var $checkboxes = $('input[type=checkbox]')
$checkboxes.click(function() {
var $other = $checkboxes.not(this);
...
});
});
$(function() {
var checkboxes = $('input[type=checkbox]');
checkboxes.click(function() {
var notclicked = checkboxes.not(this);
}
});
$(function() {
var $checkboxes = $('input[type=checkbox]')
$checkboxes.click(function() {
var $other = $checkboxes.not(this);
...
});
});