输入属性可以具有相同的“名称” [英] Input attributes that can have the same "name"
问题描述
我注意到,如果你有几个收音机在一起,你需要使它们的name属性相同,以使收音机按预期工作:
< label for =a1>< input type =radioname =aid =a1value =1> 1<标签>
< label for =a2>< input type =radioname =aid =a2value =2> 2< / label>
< label for =a3>< input type =radioname =aid =a3value =3> 3< / label>
< label for =a4>< input type =radioname =aid =a4value =4> 4< / label>
无线电广播输入是唯一可以重复名称属性的输入类型)?如果我在任何其他输入上这样做,它会被浏览器视为无效,对吗?
我问这是因为我需要在脚本中处理这种情况,并想知道在处理多个相同的名称时是否应该考虑其他输入类型。
用户交互的角度来看, input:radio
元素使用相同的 [name]
,以便浏览器知道只允许一个成为:一次检查
。
从表单提交的角度来看,任何元素都可以具有相同的名称,它们都将被序列化为查询字符串,如 HTML规范
以下是一些示例:
< form action =/ foo / bar>
< input type =hiddenname =fizzvalue =buzz/>
< input type =radioname =foovalue =bar/>
< input type =radioname =foovalue =baz/>
< input type =submitvalue =Go/>
< / form>
提交此表格(使用栏
收音机按钮选中)将会产生一个查询字符串:
?fizz = buzz& foo = bar
但是,如果将 input:hidden
元素的名称更改为 foo
:
< form action =/ foo / bar> ;
< input type =hiddenname =foovalue =buzz/>
< input type =radioname =foovalue =bar/>
< input type =radioname =foovalue =baz/>
< input type =submitvalue =Go/>
< / form>
查询字符串为:
?foo = buzz& foo = bar
服务器应正确解析这个,以便您可以同时获得 buzz
和 bar
值,但是我发现有些服务器端语言在查询字符串解析方面有些怪异。
如果密钥后缀为 []
:
?foo [] = buzz& foo [] = bar
会有 $ _ GET ['foo'] = array('buzz','bar');
I noticed that if you have a couple of radios together, you are required to make the name attribute identical on all of them in order for the radios to work as expected:
<label for="a1"><input type="radio" name="a" id="a1" value="1">1</label>
<label for="a2"><input type="radio" name="a" id="a2" value="2">2</label>
<label for="a3"><input type="radio" name="a" id="a3" value="3">3</label>
<label for="a4"><input type="radio" name="a" id="a4" value="4">4</label>
Is the radio input the only input type where you can have duplicate name attributes (and required to do so)? If I do this on any other input, it would be considered invalid by the browser, right?
I'm asking this because I need to handle this situation in a script, and want to know if there are other input types I should take into consideration when dealing with multiple identical names.
From a user-interaction perspective, input:radio
elements use the same [name]
so that the browser knows to only allow one to be :checked
at a time.
From a form-submission perspective, any elements can have the same name, they will all be serialized into the query string as defined in the HTML Spec
Here are a couple examples:
<form action="/foo/bar">
<input type="hidden" name="fizz" value="buzz" />
<input type="radio" name="foo" value="bar" />
<input type="radio" name="foo" value="baz" />
<input type="submit" value="Go" />
</form>
Submitting this form (with the bar
radio button checked) will result in a query string of:
?fizz=buzz&foo=bar
However, if you change the name of the input:hidden
element to foo
:
<form action="/foo/bar">
<input type="hidden" name="foo" value="buzz" />
<input type="radio" name="foo" value="bar" />
<input type="radio" name="foo" value="baz" />
<input type="submit" value="Go" />
</form>
The querystring will be:
?foo=buzz&foo=bar
The server should correctly parse this so that you can get both buzz
and bar
values, however I've found that some server-side languages have quirks when it comes to query string parsing.
PHP in particular will turn keys into arrays if the key is suffixed with []
:
?foo[]=buzz&foo[]=bar
will have $_GET['foo'] = array('buzz', 'bar');
这篇关于输入属性可以具有相同的“名称”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!