如何在选中单选按钮时取消选中复选框,并在使用Javascript选中复选框时取消选中单选按钮

时间:2014-06-17 15:57:49

标签: javascript 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>

如果选中复选框,则应取消选中单选按钮,当选中单选按钮时,应取消选中复选框。

             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);
     });
          });

1 个答案:

答案 0 :(得分:1)

我希望这就是你要找的东西:

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/