【发布时间】:2022-01-14 10:46:20
【问题描述】:
我正在使用 2 组单选按钮,我们称之为 1.1 1.2 和 2.1 2.1(始终选中第二组的一个单选按钮,取决于第一组中的哪一个被选中,另一个被隐藏)。
我不明白为什么在选中两个单选按钮时我需要双击单选按钮来取消选中它。我只想单击一次以“取消选中”它。
function show() {
swich();
}
function swich() {
$('input[type="radio"]').change(function(){
$('[data-group="' + $(this).data('group') + '"]').prop('checked', this.checked);
});
}
var checkedradio = false;
var radioState = [];
$("input[type=radio]").on('click', function(e) {
if (radioState[this.name] === this) {
this.checked = false;
radioState[this.name] = null;
} else {
radioState[this.name] = this;
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input data-group="A" id="radio1" required type="radio" value="Yes" name="group1">
<input data-group="B" id="radio2" required type="radio" value="No" name="group1">
<div id="someId1">
<input data-group="A" id="radio3" type="radio" name="group2" value="Yes" onclick="show()">
</div>
<div id="someId2">
<input data-group="B" id="radio4" type="radio" name="group2" value="No" onclick="show()">
</div>
【问题讨论】:
标签: javascript php html jquery