2 回答

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超3個贊
您可以獲得name單選按鈕的屬性,clicked根據(jù)這個屬性,我們將遍歷所有具有該名稱的單選按鈕和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貢獻(xiàn)1757條經(jīng)驗(yàn) 獲得超8個贊
這是純 Javascript 的實(shí)現(xiàn)
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 關(guān)注
- 152 瀏覽
添加回答
舉報