使输入文本字段可更改和可选择(通过复选框切换)
请看我当前的 html 片段如下。我正在尝试使 html 输入字段可更改和可选择。因此,我可以更改输入文本字段中的颜色值,该字段通过相应的复选框进行切换。单击提交按钮时,仅提交所选颜色的信息。 我当前的html正确显示内容,但它会提交两种颜色信息。尽管只选中了一个复选框。
<form id="myForm">
<input type="checkbox" name="color1" value="blue_check" />
Color1: <input type="text" name="blue" value="120" /></br>
<input type="checkbox" name="color2" value="red_check" />
Color2: <input type="text" name="red" value="160" /></br>
<input type="submit" value="OK" />
</form>
或点击:http://jsfiddle.net/4xDFK/56/
提前谢谢您。
Please see my current html snippet is as below. I am trying to make html input fields changeable and selectable. So I can change the value of colors in input text field which is toggled with the corresponding checkbox. When the submit button is clicked, only the information of the selected color is submitted.
My current html displays the content correctly, but it will submit two colors information. Despite of only one checkbox is selected.
<form id="myForm">
<input type="checkbox" name="color1" value="blue_check" />
Color1: <input type="text" name="blue" value="120" /></br>
<input type="checkbox" name="color2" value="red_check" />
Color2: <input type="text" name="red" value="160" /></br>
<input type="submit" value="OK" />
</form>
or click on: http://jsfiddle.net/4xDFK/56/
Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 jQuery:
由于您没有提到 JavaScript 或 JS 框架的使用,因此建议使用纯 html 表单和服务器端评估来解决它:
如果您只想提交或允许根据复选框状态填充输入,则必须使用 JavaScript(并且可能应该在你的问题或者至少作为标签)。
With jQuery:
As you don't mention the use of JavaScript or a JS-framework, here is a suggestion to solve it with the pure html form and server-side evaluation:
If you only want to so submit or allow filling of the inputs according to the checkbox state, you have to use JavaScript (and should probably have mentioned that in your question or at least as a tag).
如果未选中相关复选框,您可以在提交时删除元素。
如何阻止表单发送我们不想发送一些字段
you can remove elements on submit , if related checkbox is not checked .
how to prevent form from sending some fields we don't want to send
当您单击“提交”按钮时,所有输入值都会提交。所以你需要做的是在服务器端处理这个问题。为复选框命名并检查它们的值,谷歌搜索“处理 XXXX 中的复选框”,其中 XXXX 是您的服务器端语言。
然后,您可以检查选中了哪些复选框,并相应地查看是否应该考虑相应的文本输入字段。
All input values are submitted as you click hit the submit button. So what you need to do is to handle this on the server side. Give names to the checkboxes and check their values, google for "handling checkboxes in XXXX" where XXXX is your server side language.
You can then check which checkboxes are checked, and accordingly see if you should put the corresponding text input fields into consideration.