javascript 可以具有相同“名称”的输入属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9944559/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 08:18:49  来源:igfitidea点击:

Input attributes that can have the same "name"

javascripthtmlformsinput

提问by Alex

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:

我注意到如果你有几个收音机,你需要让所有收音机的 name 属性相同,以便收音机按预期工作:

  <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.

我问这个是因为我需要在脚本中处理这种情况,并且想知道在处理多个相同名称时是否应该考虑其他输入类型。

回答by zzzzBov

From a user-interaction perspective, input:radioelements use the same [name]so that the browser knows to only allow one to be :checkedat a time.

从用户交互的角度来看,input:radio元素使用相同的元素,[name]以便浏览器知道一次只允许一个元素:checked存在。

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

从表单提交的角度来看,任何元素都可以具有相同的名称,它们都将被序列化为HTML 规范中定义的查询字符串

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 barradio button checked) will result in a query string of:

提交此表单(bar选中单选按钮)将导致查询字符串:

?fizz=buzz&foo=bar

However, if you change the name of the input:hiddenelement to foo:

但是,如果您将input:hidden元素的名称更改为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 shouldcorrectly parse this so that you can get both buzzand barvalues, however I've found that some server-side languages have quirks when it comes to query string parsing.

服务器应该正确解析它,以便您可以获得buzzbar值,但是我发现某些服务器端语言在查询字符串解析方面有一些怪癖。

PHP in particular will turn keys into arrays if the key is suffixed with []:

如果键带有后缀,PHP 尤其会将键转换为数组[]

?foo[]=buzz&foo[]=barwill have $_GET['foo'] = array('buzz', 'bar');

?foo[]=buzz&foo[]=bar会有 $_GET['foo'] = array('buzz', 'bar');

回答by Quentin

Is the radio input the only input type where you can have duplicate name attributes

单选输入是唯一可以具有重复名称属性的输入类型吗

No. Any form control can share a name with any other form control.

不可以。任何表单控件都可以与任何其他表单控件共享一个名称。

This is particularly useful for checkboxes (it allows you to say "Pick any number of these" and then loop over the results on the server without having to hard code a different name for each item.) and submit buttons (it lets you tell which one was clicked without looping over all possible names).

这对于复选框特别有用(它允许您说“选择任意数量的这些”,然后在服务器上循环结果,而不必为每个项目硬编码不同的名称。)和提交按钮(它可以让您知道哪个单击一个而不循环所有可能的名称)。

(and required to do so)?

(并且必须这样做)?

Yes. Only radio buttons get special behaviour based on shared names.

是的。只有单选按钮会基于共享名称获得特殊行为。

回答by Dunhamzzz

It is perfectly valid to have the same value for name attributes on pages.

页面上的名称属性具有相同的值是完全有效的。

A common fallback for checkboxes is to have a hidden input of the same name with a value set to false. When using the same name values tho be sure to double check the expected output, normally the latest value to be parsed will overwrite any previous parameters of the same name.

复选框的常见回退是具有相同名称的隐藏输入,其值设置为false。使用相同名称值时,请务必仔细检查预期输出,通常要解析的最新值将覆盖任何先前的同名参数。

If you need to group various fields under the same name you can actually create an array with multiple elements, eg:

如果您需要以相同的名称对各种字段进行分组,您实际上可以创建一个包含多个元素的数组,例如:

<input name="list[]" />
<input name="list[]" />
<input name="list[]" />

回答by AaronLS

You can also have multiple hidden inputs of the same name. As pointed out it is a matter of how the server side framework will parse them. In .NET MVC the model binder will look for a collection of the same name in the parameter of the post action method or a property on the view model parameter of the post action. Such as List<int>, List<Guid>, or List<string>

您还可以有多个同名的隐藏输入。正如所指出的,这是服务器端框架如何解析它们的问题。在 .NET MVC 中,模型绑定器将在 post action 方法的参数或 post action 的视图模型参数的属性中查找同名集合。例如List<int>, List<Guid>, 或List<string>

See as an example: https://stackoverflow.com/a/2013915/84206

举个例子:https: //stackoverflow.com/a/2013915/84206

回答by ControlAltDel

Well, technically all that matters is the URL string generated. So you could theoretically have two submit buttons with the same name...

好吧,从技术上讲,重要的是生成的 URL 字符串。所以理论上你可以有两个同名的提交按钮......

回答by Maziar Aboualizadehbehbahani

no, some other controls are exist with dup name;)

不,还有一些其他控件与 dup 名称一起存在;)

回答by Ashok Raj

some elements names or attributes when used multiple times are just ignored by the HTML parser
For example if you use more than one id only the first is considered.

一些元素名称或属性在多次使用时会被 HTML 解析器忽略
。例如,如果您使用多个 id,则只考虑第一个。