2012-02-20 1 views
0

나는 라디오 버튼으로 ul을 제어하려고했습니다. 버튼을 체크하면, 체크 된 라디오 버튼이 포함 된 최종 (다른) li과 li을 제외한 모든 li가 사라진다. 나는 아주 가깝다고 생각하지만 체크 라디오 버튼의 리가 숨겨지는 것을 배제하는 방법을 찾을 수는 없다. 내 생각 엔 .not (this) .parent ('li') 섹션이 올바른 요소를 대상으로 지정하지 않지만 대신 있어야하는 부분을 해결할 수 없습니다. 여기까지 내가 만든 자바 스크립트가 있습니다.jquery가있는 trigger 요소가 포함 된 li을 제외한 목록의 모든 요소를 ​​대상으로 지정하는 방법

$('input.addressradio').change(function(){ 
    $(this).parents('ul.address').find('li').not(this).parent("li").hide(); 
    $(this).parents('ul.address').find('li.allbutonetarget').show(); 
    return false; 
}); 

그리고 여기에 내가 권하고 싶은 html이 있습니다.

<ul class="address collectionaddressnewhideY"> 
<li> 
<input value="saved1" type="radio" class="include oneofmanyfieldset addressradio" name="collectionaddressradio" id="collectionaddressradiosaved1"></input> 
<span class="collectionaddresssaved1 savedaddress"> 
</span> 
</li> 
<li> 
<input value="saved2" type="radio" class="include oneofmanyfieldset addressradio" name="collectionaddressradio" id="collectionaddressradiosaved2"></input> 
<span class="collectionaddresssaved2 savedaddress"> 
</span> 
</li> 
<li><input value="saved3" type="radio" class="include oneofmanyfieldset addressradio" name="collectionaddressradio" id="collectionaddressradiosaved3"></input> 
<span class="collectionaddresssaved3 savedaddress"> 
</span> 
</li> 
<li class="allbutonetarget"><input value="other" type="radio" class="include oneofmanyfieldset addressradio" name="collectionaddressradio" id="collectionaddressradioother">Other </input> 
<span> 
</span> 
</li> 
</ul> 

누군가가 올바른 방향으로 나를 가리킬 수 있고 이것이 잘못되어 가고있는 곳을 알려주면 정말 감사 할 것입니다.

답변

0
$('input.addressradio').on('change', function(){ 
    $(this).parent().siblings('li').not(this).not('.allbutonetarget').hide(); 
}); ​ 
0

.not() 함수는 DOM 요소 또는 테스트 할 DOM 요소가 포함 된 jQuery 객체를 사용할 수 있습니다. 당신이 this의 부모를 제외 할 경우에, 당신은 지금처럼 .not() 기능에 $(this).parent()를 전달하려는 :

$('input.addressradio').change(function(){ 
    $(this).parents('ul.address').find('li').not($(this).parent("li")).hide(); 
    $(this).parents('ul.address').find('li.allbutonetarget').show(); 
    return false; 
}); 

이 코드가 잘못가는 이유에 좀 더 세부 사항으로 이동하려면, 당신이지고 있었다 <li> 요소의 집합을 만든 다음 this (<input> 요소이므로 this (상위 요소 인 <li> 요소)의 부모 요소가 아니라 원본 요소 집합이 될 수 없으므로 원본 세트에 포함될 수 있습니다. 그 세트에서. 어떤 요소도 this과 일치하지 않으므로 (그들은 어떻게 될까요? 그들은 같은 유형이 아닙니다), 전체 집합은 .not()에 대한 호출에 의해 반환되었으며 모두 숨겨졌습니다.

+0

솔루션을 제공해 주셔서 감사 드리며 특히 나에게 왜 잘못 되었나요? – cryotorched

0

당신은 당신의 li의에서 라디오 버튼에서이 제외되지만되지 않습니다

$(this).parents('ul.address').find('.addressradio').not(this).parent("li").hide() 
: 먼저 모든 라디오를 찾아야한다
$(this).parents('ul.address').find('li').not(this).parent("li").hide() 

는,이를 제외하고 부모를 찾아
0

어떨까요 :

$('input.addressradio').click(function(){ 
    $('li').hide(); 
    $(this).parent().show(); 
    $('input[value="other"]').parent().show(); 
}) 

jsFiddle 예. li

0
$('input.addressradio').change(function() { 
    $(this).parent().siblings('li').not('.allbutonetarget').hide(); 
    return false;//keeps this one from being checked? you want that? 
}); 
  • 상위 태그 형제 선택기이다.
  • .not().hide에서 하나 (그 클래스와 함께)를 제외합니다.
  • 현재 확인 된 라디오는 정의로 인해 이라는 형제가 아니므로 제외됩니다.
관련 문제