2015-10-05 12 views
-4

在多選問題中,如果選中最後一個複選框(「以上都不是」),如何取消選中所有選中的選項(如果有的話)?如何使複選框在javascript中獨佔?

+1

你有問題的例子嗎?如果你發佈一些代碼,它會更容易得到答案... – WhiteHat

+2

你到目前爲止嘗試過什麼,相關的源代碼在哪裏與你的問題一起去? – NewToJS

+0

銳步
耐克
<輸入型=「checkbox」name =「brand」value =「Puma」> Puma
Levis
這些都不是

回答

2

它取決於你的HTML結構,但我可以向您的例子

$('input').change(function() { 
 
    var $el = $(this); 
 
    
 
    if ($el.attr('id') == 'none' && $el.prop('checked')) { 
 
    $('input').not($el).prop('checked', false); 
 
    } else { 
 
    $('#none').prop('checked', false); 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div> 
 
    <input id="op1" type="checkbox" /><label for="op1">Option 1</label> 
 
    <input id="op2" type="checkbox" /><label for="op2">Option 2</label> 
 
    <input id="op3" type="checkbox" /><label for="op3">Option 3</label> 
 
    <input id="op4" type="checkbox" /><label for="op4">Option 4</label> 
 
    <input id="op5" type="checkbox" /><label for="op5">Option 5</label> 
 
    <input id="none" type="checkbox" /><label for="none">None</label> 
 
</div>

+0

這是JQuery,我問了javascript。 –

+0

完成!

Reebok
input type =「checkbox」name =「brand」value =「Puma」> Puma
Levis
這些都不是

0

可以得到應用在所有的複選框的通用類名,然後使用下面的代碼:

$('.checkboxCSS').click(function(){ 
    if($(this).attr('id') == 'none') 
      $('.checkboxCSS').not(this).attr('checked', false); 
    }); 
+0

這是jQuery,我問javascript。 –

+0

完成!

Reebok
input type =「checkbox」name =「brand」value =「Puma」> Puma
Levis
這些都不是

0

您可以藉助以下代碼:

小提琴鏈接:http://jsfiddle.net/sqz75b9g/12/

HTML:

<div class="row"> 
    <input type="checkbox" class="selectanswer"/>select 0 
    <input type="checkbox" class="selectanswer"/>select 1 
    <input type="checkbox" class="selectanswer"/>select 2 
    <input type="checkbox" class="selectanswer"/>select 3 
    <input type="checkbox" class="selectanswer"/>None of the above 
</div> 

的Javascript:

$(document).ready(function(){ 
    $(".selectanswer").click(function(){ 
     var currentParent = $(this).closest(".row"); 
     var checkboxGroup = $(currentParent).find(".selectanswer"); 
     if($(this).prop("checked") && $(this).index() == checkboxGroup.length-1) 
     { 
      $(checkboxGroup).not(this).prop('checked', false); 
     } 
     else 
     { 
      $(checkboxGroup).eq(checkboxGroup.length-1).prop('checked', false); 
     } 
    }); 
}); 
+0

這是jQuery,我問javascript。 –

+0

@ManikManocha - 爲什麼你爲你的問題添加了'jquery'標籤。你絕對是在浪費別人的時間。在發佈您的下一個問題時請記住這一點。 – vijayP

+0

您應該閱讀整個問題 –

0

這是我會怎麼做:

$("[name='none']").click(function(){ 
    if($(this).attr('checked')=="checked"){ 
     $(this).siblings().attr('checked', false); 
    } 
}); 

Here is the JSFiddle demo

+0

這是jQuery,我問javascript。 –

+1

完成!

Reebok
input type =「checkbox」name =「brand」value =「Puma」> Puma
Levis
這些都不是