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');
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…