HTML通过一个复选框提交多个值? [英] HTML submit multiple values through one check box?
问题描述
您好,我有一个表格,允许用户检查任意数量的选项,然后单击提交".有什么方法可以使输入类型复选框"提交多个值?
Hello I have a form that allows the user to check as many options as they like and then hit submit. Is there any way to have the input type 'checkbox' submit more than one value?
例如,现在我有:
<input type="checkbox" value="testuser">
但是我想要类似的东西
<input type="checkbox" value="testuser" valuetwo="1">
有没有办法实现第二种选择? 谢谢!
Is there any way to achieve this second option? Thanks!
由于无法提交值,是否有办法同时提交两个值? 例如:
Since there is no way to submit to values, is there a way to submit them both in value one? For example:
<input type="checkbox" value="testuser,1">
如果是的话,我该如何将值一分为二?
And if so how would I separate the value into two?
推荐答案
从您的评论看来,您似乎拥有一些可以在提交数据之前处理数据的JavaScript.在这种情况下,您可以将数据属性添加到复选框.要使用您的示例,可以将其命名为data-valuetwo
.
From your comments, it sounds like you have some JavaScript that handles the data before it's submitted. If that's the case, you can add a data attribute to the checkbox. To use your example, you could call it data-valuetwo
.
<input type="checkbox" value="testuser" data-valuetwo="1">
然后,您的JavaScript可以使用getAttribute
来检索data-valuetwo
属性中的值并适当地对其进行处理.看起来可能像这样:
Then, your JavaScript can use getAttribute
to retrieve the value in your data-valuetwo
attribute and handle it appropriately. It could look something like this:
var valuetwo = checkbox.getAttribute("data-valuetwo");
这篇关于HTML通过一个复选框提交多个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!