function uncheck() {
var notTest = document.getElementById("choice_31_3_2");
var Test = document.getElementById("choice_31_3_1");
if (notTest.checked) {
Test.checked = false;
}
if (Test.checked) {
notTest.checked = false;
}
}
jQuery("#choice_31_3_1").click(uncheck);
jQuery("#choice_31_3_2").click(uncheck);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input name="input_3.1" value="Test" id="choice_31_3_1" type="checkbox">
<label for="choice_31_3_1" id="label_31_3_1">Test</label>
<input name="input_3.2" value="notTest" id="choice_31_3_2" type="checkbox">
<label for="choice_31_3_2" id="label_31_3_2">notTest</label>
我写了一个函数来取消选中另一个复选框,我正在使用 jQuery 在这些特定输入上调用 uncheck()。
我得到了我想要的结果。当我检查测试然后检查 notTest 时,测试未被选中。但是当我再次按下测试时,测试复选框拒绝检查,除非我手动取消选中 notTest。
我包含了代码片段,请问有什么问题吗?
代码在 Wordpress 上运行正常,但不幸的是这里不行。
最佳答案
给你一个解决方案
$('input[type="checkbox"]').change(function(){
console.log($(this).is(':checked'));
if($(this).is(':checked')){
$(this).siblings('input[type="checkbox"]').attr('checked', false);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input name="input_3.1" value="Test" id="choice_31_3_1" type="checkbox">
<label for="choice_31_3_1" id="label_31_3_1">Test</label>
<input name="input_3.2" value="notTest" id="choice_31_3_2" type="checkbox">
<label for="choice_31_3_2" id="label_31_3_2">notTest</label>
希望对您有所帮助。
关于javascript - 选中另一个复选框时取消选中一个复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47088994/