2015-10-16 169 views
1

我在調查中有兩種問題。用戶可以在每個問題中選擇多個選項,然後用戶只需做出一個選擇的其他問題。我不想使用單選按鈕。到目前爲止,我已經完成了這個工作,但這不起作用。如何添加複選框選擇只有一個選擇?

<ol class = "single"> 
    <li> question: question one</li> 
    <li><input type="checkbox"> choice one</li> 
    <li><input type="checkbox"> choice two</li> 
    <li><input type="checkbox"> choice three</li> 
</ol> 

<ol class = "multiple"> 
    <li> question: question two </li> 
    <li><input type="checkbox"> choice one </li> 
    <li><input type="checkbox"> choice two </li> 
    <li><input type="checkbox"> choice three</li> 
</ol> 

我jQuery代碼:

$('.single').each(function(){ 
    $('input:checkbox').click(function(){ 
    if ($(this).is(':checked')) { 
     $('input:checkbox').not(this).prop('checked', false); 
    } 
    }); 
}); 

回答

0

您不必使用each,只是拉近您正在使用的單擊事件綁定選擇的範圍:

$('.single').find('input:checkbox').click(function() { 
    var $this = $(this); 
    $this.closest('.single').find('input:checkbox').not($this).prop('checked', false); 
}); 
+0

仍然無法使其工作。它選擇所有這些。 – Ann

+0

立即嘗試編輯我的答案@安寧 – taxicala

+0

仍然一樣。 – Ann

相關問題