【发布时间】:2021-06-28 20:19:35
【问题描述】:
找不到答案:
我搜索了 Stack Overflow,但是,尽管找到了很多类似的帖子——以及更复杂的情况——我仍然找不到我想要解决的问题的答案。
这是我的问题:
我有四个单选按钮和一个隐藏字段:
<!-- My HTML Document -->
<form action="/my-doc.html" method="post">
<!-- The 4 Radio Buttons-->
<input type="radio" name="game" value="1" checked> First
<input type="radio" name="game" value="2"> Second
<input type="radio" name="game" value="3"> Third
<input type="radio" name="game" value="4"> Fourth
<!-- The Hidden Field -->
<input type="hidden" name="criteria" value="1">
<!-- My Submit Button -->
<input type="submit" name="action" value="Go">
</form>
我需要做的是将<input type="hidden" name="criteria" value="1">的值设置为0
像这样:<input type="hidden" name="criteria" value="0">
...但仅在用户选择第一个或第二个单选按钮之后。如果选择了任何其他单选按钮,则隐藏字段的值应保持等于 1。
人们如何使用 JavaScript 做到这一点?
要求:“寻找 VanillaJS 的答案。”
【问题讨论】:
标签: javascript html forms