2 回答
TA贡献1828条经验 获得超3个赞
您可以获得name单选按钮的属性,clicked根据这个属性,我们将遍历所有具有该名称的单选按钮和disable未选中的单选按钮。
这是演示代码:
$("input[type=radio]").click(function() {
//getting name attribute if radio which is clicked
var name = $(this).attr("name");
console.log(name)
//loop only through those radio where name is same
$('input[name="' + name + '"]').each(function() {
//if not selected
if ($(this).is(":not(:checked)")) {
// add disable
$(this).attr('disabled', 'disabled');
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="question_radio" type="radio" name="ans" value="<?php echo $result['id']; ?>" /> A
<input id="question_radio" type="radio" name="ans" value="<?php echo $result['id']; ?>" />B
<input id="question_radio" type="radio" name="ans" value="<?php echo $result['id']; ?>" />C
<br>
<input id="question_radio" type="radio" name="ans1" value="<?php echo $result['id']; ?>" />A
<input id="question_radio" type="radio" name="ans1" value="<?php echo $result['id']; ?>" /> B
<input id="question_radio" type="radio" name="ans1" value="<?php echo $result['id']; ?>" />C
TA贡献1757条经验 获得超8个赞
这是纯 Javascript 的实现
document.querySelectorAll("input[type=radio]").forEach(function (el) {
el.addEventListener('click', function () {
//getting name attribute if radio which is clicked
var name = el.getAttribute('name');
//loop only through those radio where name is same.
document.querySelectorAll('input[name="' + name + '"]').forEach(function (el) {
if (el.matches(":not(:checked)")) {
el.setAttribute('disabled', 'disabled');
}
});
});
});
- 2 回答
- 0 关注
- 123 浏览
添加回答
举报