簡體   English   中英

如何在選中單選按鈕時取消選中復選框,以及在使用Javascript選中復選框時如何取消選中單選按鈕

[英]How to uncheck checkbox when radio button is checked and uncheck radio button when checkboxes are checked using Javascript

<div id="a">
  <input type="radio" name="sex" value="male">Male<br>
  <input type="radio" name="sex" value="female">Female
</div>

<div id="b">
  <input type="checkbox" name="vehicle" value="Bike">I have a bike<br>
  <input type="checkbox" name="vehicle" value="Car">I have a car 
</div>

如果選中了復選框,則應取消選中單選按鈕,而選中單選按鈕時,則應取消選中選中的復選框。

             function uncheckDisabiltiesCheckbox(){
     var isChecked1 = document.getElementById('disabilitiesCheckbox1').checked;
     var isChecked2 = document.getElementById('disabilitiesCheckbox2').checked;
     var isChecked3 = document.getElementById('disabilitiesCheckbox3').checked;
     var isChecked4 = document.getElementById('disabilitiesCheckbox4').checked;
     var isChecked5 = document.getElementById('disabilitiesCheckbox5').checked;
     var isChecked6 = document.getElementById('disabilitiesCheckbox6').checked;
     var isChecked7 = document.getElementById('disabilitiesCheckbox7').checked;
     var isChecked8 = document.getElementById('disabilitiesCheckbox8').checked;
     var isChecked9 = document.getElementById('disabilitiesCheckbox9').checked;
     var isChecked10=document.getElementById('disabilitiesCheckbox10').checked;
             var isChecked11=document.getElementById('disabilitiesCheckbox11').checked;
             var isChecked12=document.getElementById('disabilitiesCheckbox12').checked;
             var isChecked13=document.getElementById('disabilitiesCheckbox13').checked;               


     if(isChecked1){
         document.getElementById('disabilitiesCheckbox1').checked = false;
     }
     if(isChecked2){
         document.getElementById('disabilitiesCheckbox2').checked = false;
     }
     if(isChecked3){
         document.getElementById('disabilitiesCheckbox3').checked = false;
     }
     if(isChecked4){
         document.getElementById('disabilitiesCheckbox4').checked = false;
     }
     if(isChecked5){
         document.getElementById('disabilitiesCheckbox5').checked = false;
     }
     if(isChecked6){
         document.getElementById('disabilitiesCheckbox6').checked = false;
     }
     if(isChecked7){
         document.getElementById('disabilitiesCheckbox7').checked = false;
     }
     if(isChecked8){
         document.getElementById('disabilitiesCheckbox8').checked = false;
     }
     if(isChecked9){
         document.getElementById('disabilitiesCheckbox9').checked = false;
     }
     if(isChecked10){
         document.getElementById('disabilitiesCheckbox10').checked = false;
     }
     if(isChecked11){
         document.getElementById('disabilitiesCheckbox11').checked = false;
     }
     if(isChecked12){
         document.getElementById('disabilitiesCheckbox12').checked = false;
     }
     if(isChecked13){
         document.getElementById('disabilitiesCheckbox13').checked = false;
     }
   }

上面的代碼是選中單選按鈕時取消選中所有復選框的代碼。 選中復選框后如何取消選中單選按鈕。

disabilities.js

             $(document).ready(function(){

     $('#disabilitiesRadio :radio').click(function(){
           $('#disabilitiesCheckbox :checkbox').prop('checked', false);
     });

     $('#disabilitiesCheckbox :checkbox').click(function(){
         $('#disabilitiesRadio :radio').prop('checked', false);
     });
          });

我希望這是您想要的:

HTML:

<div id="a">
  <input type="radio" name="sex" value="male">Male<br>
  <input type="radio" name="sex" value="female">Female
</div>

<div id="b">
  <input type="checkbox" name="vehicle" value="Bike">I have a bike<br>
  <input type="checkbox" name="vehicle" value="Car">I have a car 
</div>

JS:

$('input[type="radio"]').change(function() {
    if ($(this).is(':checked')){ //radio is now checked
        $('input[type="checkbox"]').prop('checked', false); //unchecks all checkboxes
    }
});

$('input[type="checkbox"]').change(function() {
    if ($(this).is(':checked')){
        $('input[type="radio"]').prop('checked', false);
    }
});

小提琴:

http://jsfiddle.net/24kg2/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM