2013-04-26 28 views
0

JSFiddle如何使用.nextAll()(或其他方式)當前

後清除其他無線輸入我希望能夠明確的是,是我的父母DIV下載的兄弟姐妹,所有單選按鈕。例如,如果選擇1廣播的按鈕選擇2被選擇時,我想清除在單選按鈕選擇3和單選按鈕選擇的值4.

$('input:radio[name=radio-choice-2]').change(
    function() { 
     if ($(this).val() == 'choice-1') { 
      $(this).parent().nextAll('div :radio').removeAttr('checked'); 
     } 
    }); 
<form method="post" action=""> 
    <div id="question-1"> 
     <h4>Radio Button Choice 1</h4> 

     <label for="radio-choice-1-a" class="checkbox inline">Choice 1 
      <input type="radio" name="radio-choice-1" id="radio-choice-1-a" tabindex="2" value="choice-1" /> 
     </label> 
     <label for="radio-choice-1-b" class="checkbox inline">Choice 2 
      <input type="radio" name="radio-choice-1" id="radio-choice-1-b" tabindex="3" value="choice-2" /> 
     </label> 
    </div> 
    <div id="question-2" class="collapse-group"> 
     <h4>Radio Button Choice 2</h4> 

     <label for="radio-choice-2-a">Choice 1</label> 
     <input type="radio" name="radio-choice-2" id="radio-choice-2-a" tabindex="2" value="choice-1" /> 
     <label for="radio-choice-2-b">Choice 2</label> 
     <input type="radio" name="radio-choice-2" id="radio-choice-2-b" tabindex="3" value="choice-2" /> 
    </div> 
    <div id="question-3" class="collapse-group"> 
     <h4>Radio Button Choice 3</h4> 

     <label for="radio-choice-3-a">Choice 1</label> 
     <input type="radio" name="radio-choice-3" id="radio-choice-3-a" tabindex="2" value="choice-1" checked /> 
     <label for="radio-choice-3-b">Choice 2</label> 
     <input type="radio" name="radio-choice-3" id="radio-choice-3-b" tabindex="3" value="choice-2" /> 
    </div> 
    <div id="question-4" class="collapse-group"> 
     <h4>Radio Button Choice 4</h4> 

     <label for="radio-choice-4-a">Choice 1</label> 
     <input type="radio" name="radio-choice-4" id="radio-choice-4-a" tabindex="2" value="choice-1" checked /> 
     <label for="radio-choice-4-b">Choice 2</label> 
     <input type="radio" name="radio-choice-4" id="radio-choice-4-b" tabindex="3" value="choice-2" /> 
    </div> 
</form> 

回答

0
$('input:radio').change(function() { 
    $(this).parents('div').nextAll('div').each(function() { 
     $(this).find(':input').prop('checked',false); 
    }); 
}); 

jsFiddle example

+0

我想你想'.prop('checked',false)''而不是'.removeAttr('checked')' – Blazemonger 2013-04-26 15:48:31

+0

@Blazemonger - 的確如此,謝謝。 – j08691 2013-04-26 15:49:57

1

嘗試

$('input:radio').change(function() { 
    $(this).closest('div[id^="question"]').nextAll('div[id^="question"]').find(':radio').prop('checked', false) 
}); 

演示:Fiddle

0

所以問題在於,使用傳統的DOM遍歷方法,您需要沿樹遍歷,然後再遍歷樹 - 然而,這個函數並不是真正固有的上下多少。你想要的是將DOM看作一個線性序列,然後選取所討論的所有單選按鈕 - 不管它們是如何嵌套的。

我寫了一個插件,正好回來了:jQuery sequential traversal

利用這一點,代碼即可獲得你的收音機如下之後:

$(this).sequentialNextAll(':radio').removeAttr('checked'); 

I forked your fiddle表現出來的差異多麼少呈現給您的原件。

相關問題