gpt4 book ai didi

javascript - 如何使用 Javascript 在选中单选按钮时取消选中复选框以及在选中复选框时取消选中单选按钮

转载 作者:行者123 更新时间:2023-12-03 12:24:39 24 4
gpt4 key购买 nike

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

上面是当选择单选按钮时取消选中所有复选框的代码。如何开始在选中复选框时取消选中单选按钮。

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

fiddle :

http://jsfiddle.net/24kg2/

关于javascript - 如何使用 Javascript 在选中单选按钮时取消选中复选框以及在选中复选框时取消选中单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24268340/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com