2011-11-07 2 views
2

js 코드의 coments를 참조하십시오. 당신은 단지 현재 클릭 항목을 필터링 .not를 사용하려면다른 체크 상자를 찾으십시오

$('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(); 

}); 

답변

2

:

나는이 확인란 여기

<input type="checkbox" name="first" id="first" /> 
<input type="checkbox" name="second" id="second" /> 

이 나의 자바 스크립트

$(function() { 
    $('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 ? 

     /* 
    } 
    }); 
}); 
2

당신은 할 수있다 (this)

$(function() { 
    var checkboxes = $('input[type=checkbox]'); 
    checkboxes.click(function() { 
     var notclicked = checkboxes.not(this); 
    } 
}); 

http://jsfiddle.net/infernalbadger/amz7f/

1

당신은 현재/클릭 체크 박스를 제외 할 .not() 기능을 사용할 수 있습니다. 예.

$(function() { 
    var $checkboxes = $('input[type=checkbox]') 

    $checkboxes.click(function() { 
     var $other = $checkboxes.not(this); 
     ... 
    }); 
}); 
관련 문제