简体   繁体   English

fieldset元素的name属性有什么作用?

[英]What does the fieldset element's name attribute do?

I understand the general semantics of a <fieldset> , along with how the name attribute on a <fieldset> can be used to give meaning to a group of inputs. 据我所知的一般语义的<fieldset> ,以及如何在name上属性<fieldset>可用于赋予意义的一组输入。 The W3 wiki has a number of examples with this. W3 wiki有很多例子。

However, I don't understand what the name attribute on <fieldset> elements do when submitting forms. 但是,我不明白提交表单时<fieldset>元素的name属性是做什么的。 According to MDN , the name is submitted with the form data. 根据MDNname随表单数据一起提交。 W3C also mentions that it's for the element's name, which is used in form submission. W3C还提到它是元素的名称,用于表单提交。

When trying out the name attribute on a <fieldset> though, I don't see it being submitted with the rest of the form data. 在尝试<fieldset>上的name属性时,我看不到它与其余的表单数据一起提交。 It's unclear to me if it has any use other than semantics. 我不清楚它是否有语义之外的任何用途。

Is the name attribute on <fieldset> elements supposed to be submitted with the form data? 是否应该使用表单数据提交<fieldset>元素的name属性? If so, does it have a value? 如果是这样,它有价值吗? What does it do? 它有什么作用?

However, I don't understand what the name attribute on <fieldset> elements do when submitting forms. 但是,我不明白提交表单时<fieldset>元素的名称属性是做什么的。

Nothing. 没有。

You've tested this yourself: 你自己测试过:

When trying out the name attribute on a <fieldset> though, I don't see it being submitted with the rest of the form data. 在尝试<fieldset>上的name属性时,我看不到它与其余的表单数据一起提交。

It appears to be an error in the summary of the spec (presumably copied from input at some point) which has been copied onto the MDN reference. 它似乎是规范摘要中的错误(可能是从某个点的input复制而来),它已被复制到MDN参考上。

The rules for form submission don't mention fieldsets. 表单提交规则没有提到字段集。

W3C also mentions that it's for the element's name, which is used in form submission. W3C还提到它是元素的名称,用于表单提交。

The complete quote is: 完整的报价是:

name - Name of form control to use for form submission and in the form.elements API name - 用于表单提交和form.elements API的表单控件的名称

The form.elements API (along with generic DOM APIs like getElementsbyName ) is the only place where the attribute has any actual effect. form.elements API(以及getElementsbyName等通用DOM API)是属性具有任何实际效果的唯一位置。

This is a bit strange to me, but it seems the purpose is so that you can access it via myForm.elements like you can any other form element. 这对我来说有点奇怪,但似乎目的是你可以通过myForm.elements访问它,就像你可以使用任何其他表单元素一样。 Fieldsets do have some relevance to how your form functions (for example, making a fieldset "disabled" will apply to all its child form controls), so I guess this is something some developer might want to do. 字段集确实与表单函数的相关性有关(例如, 使字段集“禁用”将应用于其所有子表单控件),所以我想这是某些开发人员可能想要做的事情。

I'm pretty sure that's the only use for it. 我很确定这是它的唯一用途。 Fieldsets can't have a value (even if you set one, your browser should ignore it and not submit it), so it'll never be included in what is sent to the server. 字段集不能有值(即使您设置了一个值,您的浏览器也应该忽略它而不提交它),因此它永远不会包含在发送到服务器的内容中。

Here's a little test I made trying to understand this: 这是我试图理解这个的一个小测试:

 var fruitform = document.getElementById("fruitform"); var output = document.getElementById("output"); function log(msg) { output.innerHTML = output.innerHTML + "\\n" + msg; } log("*** fruitform.elements ***") log(JSON.stringify(fruitform.elements)); log(""); 
 <h2>test form</h2> <!-- the 'action' url' is just a page that outputs any GET parameters you pass to it --> <form action="http://www.w3schools.com/html/action_page.php" id="fruitform"> <fieldset name="facts"> type: <input type="text" name="type" value="banana"> <br><br> color: <input type="text" name="color" value="yellow"> </fieldset> <br> <input type="submit"> </form> <!-- place to put some output --> <h2>output</h2> <pre id="output"> </pre> 

( here's the same demo on codepen ) 这是codepen上的相同演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM