Html 有两个同名的输入元素是否有效?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2906793/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it valid to have two input elements with the same name?
提问by Rohan
i.e.:
IE:
<form 1>
<input type="hidden" name="url" value="1">
</form 1>
and
和
<form 2>
<input type="hidden" name="url" value="2">
</form 2>
Is this allowed and valid?
这是允许和有效的吗?
采纳答案by Rohan
Yes,it is valid
是的,这是有效的
This is Good
这很好
<form name="form1">
<input type="hidden" name="url" value="1">
</form>
<form name="form2">
<input type="hidden" name="url" value="2">
</form>
This is also fineand will generally be interpreted as an array of values, e.g. {url: [1, 2]}
, depending on what your server does. In a URL encoding, it will look like url=1&url=2
.
这也很好,通常会被解释为一组值,例如{url: [1, 2]}
,取决于您的服务器做什么。在 URL 编码中,它看起来像url=1&url=2
.
<form name="form1">
<input type="hidden" name="url" value="1">
<input type="hidden" name="url" value="2">
</form>
回答by Quentin
Yes.
是的。
More, it is essential if you are dealing with radio button groups.
更重要的是,如果您正在处理单选按钮组。
回答by Jeff Lowery
"This is Not Good" parses correctly on every browser I know of; if two url's appear in the url encoded string, it will be treated as an array. Try this in JQuery:
“这不好”在我知道的每个浏览器上都能正确解析;如果两个 url 出现在 url 编码字符串中,它将被视为一个数组。在 JQuery 中试试这个:
$('<form name="form1">\
<input type="hidden" name="url" value="1">\
<input type="hidden" name="url" value="2">\
</form>').serialize()
and you will get: "url=1&url=2"
你会得到: "url=1&url=2"
a well-written query string parser will return a json structure like this:
一个编写良好的查询字符串解析器将返回一个 json 结构,如下所示:
{"url":["1", "2"]}
Is it strictly spec? Nope, but neither is creating a multi-line string by escaping the EOL with a backslash, as I did above.
它是严格规范吗?不,但也没有像我上面那样通过用反斜杠转义 EOL 来创建多行字符串。
回答by Kerry Jones
Yes -- each will only submit with their respective forms.
是的 - 每个人只会提交各自的表格。
If you have them in the same form, one will override the other and it is not valid.
如果它们以相同的形式存在,一个将覆盖另一个并且无效。
回答by Wassim AZIRAR
To test if it is valid or not, creat you page and test at W3C here :
要测试它是否有效,请在此处创建页面并在 W3C 进行测试:
回答by Aksahy N Shelke
A) Your first example is okay, because the forms time of submission will be different:
A) 你的第一个例子没问题,因为表单提交的时间会有所不同:
<form id="1">
<input type="hidden" name="url" value="1">
</form>
<form id="2">
<input type="hidden" name="url" value="2">
</form>
B) Your second example is also okay, but not standard coding practice:
B)你的第二个例子也可以,但不是标准的编码实践:
<form>
<input type="hidden" name="url" value="1">
<input type="hidden" name="url" value="2">
</form>
Java code two extract both values:
Java 代码二提取两个值:
Map<String,String[]> parmMap = requestObj.getParameterMap();
String input1 = parmMap.get("url")[0];
String input2 = parmMap.get("url")[1];