gpt4 book ai didi

jquery - 检查从每一组中至少选择一个单选按钮(使用 jquery)

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

我在表单上动态拉动和显示单选按钮问题(通过 jquery 选项卡分为不同的部分)。

所以有很多 2、3、4、5 等单选按钮在一起,它们具有相同的“名称”但不同的 id。

当您在各个部分之间点击“下一步”时,我想检查是否为每个组至少选择了一个单选按钮。

(当表单加载时,单选按钮默认没有被选中,所以必须是这样。)

我可以循环浏览当前部分的单选按钮,但我不确定如何轻松检查每个组是否已选中一个单选按钮?

最佳答案

在元素之间没有任何关系,或者如何找到组名称的情况下,我只能给你一个指针,但这应该可以工作(通过单击 id="nextButton"的元素触发检查,但显然这可以自定义为任何其他适当的事件,例如 $('form').submit()):

$(document).ready(
function(){
$('#nextButton').click(
function(){

var radioName = something; // use this if there's a relationship between the element
// triggering this check and the radio buttons

if ($('input[name='+ radioName +']:checked').length) {
// at least one of the radio buttons was checked
return true; // allow whatever action would normally happen to continue
}
else {
// no radio button was checked
return false; // stop whatever action would normally happen
}
}
);
}
);

关于jquery - 检查从每一组中至少选择一个单选按钮(使用 jquery),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4028683/

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