Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 查找其他复选框_Javascript_Jquery - Fatal编程技术网

Javascript 查找其他复选框

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

请参阅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 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);
        ...
    });
});