输入名称是否必须以相同的形式唯一? [英] Do input names have to be unique in the same form?

查看:61
本文介绍了输入名称是否必须以相同的形式唯一?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多个 fieldset 的表单.每个 fieldset 都有多个 input ,其中一些逻辑上共享一个 name 属性.

I have a form with multiple fieldsets. Each fieldset has multiple inputs, some of which would logically share a name attribute.

我查看了 MDN作为输入名称HTML 5规范,没有运气.HTML 5表单规范的4.10.19.1节没有提到唯一性要求.

I have looked on MDN for input name and the HTML 5 spec with no luck. Section 4.10.19.1 of the HTML 5 form spec does not mention a uniqueness requirement though.

例如:

<fieldset name="attendee"> 
  <input name="full-name">
</fieldset> 

<fieldset name="next-of-kin"> 
  <input name="full-name">
</fieldset>

每个 input 名称在 fieldset 中是唯一的,但在 form 中是重复的.这有效吗?

Each input name is unique within the fieldset, but duplicated within the form. Is this valid?

推荐答案

否, name 属性不需要唯一.您可能在多个输入字段中具有类似的name属性(例如,单选按钮后面的整个原理).提交表单时,现代浏览器通常会看到一系列信息.我将为您提供一个示例,在该示例中,我使用PHP来解析信息,但是重点是其他编程语言.

No, the name attributes are not required to be unique. You could have the name attribute similar in multiple input fields (the whole principle behind radio buttons for example). Modern browsers generally see an array of information when you are submitting the form. I will give you an example in which I used PHP to parse the information, but the point stands in other programming languages.

给出您的示例:

<fieldset name="attendee"> 
  <input name="full-name">
</fieldset> 

<fieldset name="next-of-kin"> 
  <input name="full-name">
</fieldset>

如果根据方法POST/GET使用 var_dump(),您将看到浏览器实际上只记住全名属性记录的最后一个值.基本上,如果您的第一个输入是 John Doe (在 attendee 字段下),而第二个输入是 John Green (在 next-亲戚字段集),无论您使用哪种方法,浏览器都只会记住 John Green .如果您使用GET方法,则仅您的URL将包含两个全名属性,而不包含实际的 $ _ GET 数组本身.

If you var_dump() depending on your method POST/GET, you will see that the browser is actually remembering only the last value recorded by the full-name attribute. Basically if your first input is John Doe (under the attendee fieldset) and your second input is John Green (under the next-of-kin fieldset), the browser will only remember John Green regardless of your method. If you use the GET method only your URL will contain both of the full-name attributes, but not the actual $_GET array itself.

如果您想同时记录两个名称,则可以将代码编辑为:

If you want to record both the names you can edit your code to:

<fieldset name="attendee"> 
  <input name="full-name[]">
</fieldset> 

<fieldset name="next-of-kin"> 
  <input name="full-name[]">
</fieldset>

通过使用 [] ,浏览器知道不记得该属性的最后一个值.现在,无论您使用哪种方法,都执行 var_dump(),您应该会看到:

By using the [] the browser knows not to remember just the last value of that attribute. Now if you do a var_dump() regardless of your method you should see:

array(1) { ["full-name"]=> array(2) { 
                          [0]=> string(8) "John Doe" 
                          [1]=> string(10) "John Green" 
                          } 
         }

如果您想更具体一点(因为您提到的一种评论是您在REST API中使用它),则可以这样编辑代码:

If by any chance you want to be more specific (since in one of the comments you were mentioning that you are using this in a REST API), you can edit your code like this:

<fieldset name="attendee"> 
  <input name="full-name[attendee]">
</fieldset> 

<fieldset name="next-of-kin"> 
  <input name="full-name[next-of-kin]">
</fieldset>

现在,无论采用哪种方法提交表单,您都将获得以下数据结构:

Now if you submit the form, regardless of the method, you will get the following data-structure:

array(1) { ["full-name"]=> array(2) { 
                          ["attendee"]=> string(8) "John Doe" 
                          ["next-of-kin"]=> string(10) "John Green" 
                          } 
         }

从此处调用此数组上的 json_encode()并获得可与您的API一起使用的实际JSON对象(例如一个波纹管)非常简单:

It is fairly simple from here to call json_encode() on this array and get an actual JSON object(like the one bellow) that you can use with your API:

{"full-name":{"attendee":"John Doe","next-of-kin":"John Green"}}

这篇关于输入名称是否必须以相同的形式唯一?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆