gpt4 book ai didi

javascript - 如何在 html 中为 radio 组列表命名 radio ?

转载 作者:行者123 更新时间:2023-11-28 12:43:39 26 4
gpt4 key购买 nike

我希望它会发送一个颜色列表。在html中,如果输入是列表,它们应该是相同的名称,但是 radio 输入的名称相同,它们将在同一组中,那么如何分隔每个 radio 组,但所有组仍然具有相同的名称,那么我可以使用循环go在服务器端获取列表的所有值?

此外,在客户端,我需要检查是否每个组都被选中,那么如何使用javascript或jquery来选择所有单选组,而不是所有单选按钮。

<form method="post">
Car A: <input type="radio" name="color[]"/>red <input type="radio" name="color[]"/>yello<br/>
Car B: <input type="radio" name="color[]"/>red <input type="radio" name="color[]"/>yello<br/>
Car C: <input type="radio" name="color[]"/>red <input type="radio" name="color[]"/>yello<br/>
<input type="submit"/>
</form>

最佳答案

每个组的所有单选按钮都应该有不同的名称。

Car A: 
<input type="radio" name="color1"/>red
<input type="radio" name="color1"/>yello<br/>

Car B:
<input type="radio" name="color2"/>red
<input type="radio" name="color2"/>yello<br/>

Car C:
<input type="radio" name="color3"/>red
<input type="radio" name="color3"/>yello<br/>

检查是否选择了所有组:

if ($('input[type="radio"]:checked').length == 3)
validAction();
else
invalidAction();

您可以将选择器限制为仅 name 属性以 color 开头的元素:

if ($('input[type="radio"][name^="color"]:checked').length == 3)

关于javascript - 如何在 html 中为 radio 组列表命名 radio ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10316376/

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