简体   繁体   English

fieldset 表单属性的真正作用是什么?

[英]What does the fieldset form attribute really do?

I am not sure what the meaning of the fieldset form attribute is.我不确定fieldset form属性的含义是什么。 MDN says: MDN 说:

This attribute takes the value of the id attribute of a <form> element you want the <fieldset> to be part of , even if it is not inside the form.该属性采用您希望<fieldset>成为其一部分的<form>元素的 id 属性值,即使它不在表单内。 Please note that usage of this is confusing — if you want the elements inside the <fieldset> to be associated with the form, you need to use the form attribute directly on those elements.请注意,它的用法令人困惑——如果您希望<fieldset>中的元素与表单相关联,您需要直接在这些元素上使用 form 属性。

which means that the fieldset 's fields are not automatically included in the form (you have to put the form attribute on each of them if you want them to be), but the fieldset is part of the form.这意味着fieldset的字段不会自动包含在form中(如果需要,您必须将form属性放在每个字段上),但fieldset是表单的一部分 What does that actually mean?这到底是什么意思?

The spec says: 规范说:

The form attribute is used to explicitly associate the fieldset element with its form owner . form 属性用于明确地将 fieldset 元素与其表单所有者相关联。

And the form owner is linked to the note并且表单所有者链接到注释

A form-associated element can have a relationship with a form element, which is called the element's form owner.表单关联元素可以与表单元素有关系,称为元素的表单所有者。

Where it ends without any explanation what relationship is supposed to be.它结束的地方没有任何解释应该是什么关系 If you are a fieldset , could you describe in well defined terms what it feels like to be in a relationship with form ?如果您是fieldset ,您能否用定义明确的术语描述与form建立关系的感觉?

Code to verify验证码

<form id="myform">
    <input name="happy" value="happy">
    <button>Send</button>
</form>

<fieldset form="myform">
    <input name="unhappy" value="unhappy">
</fieldset>

The unhappy input is not sent with the form. unhappy的输入不会随表单一起发送。

I agree, it's confusing.我同意,这很混乱。 I made a polyfill.我做了一个 polyfill。 Comments and suggestions are welcome.欢迎提出意见和建议。

function control() {
  var items = document.querySelectorAll("FIELDSET[form]");
  
  var getter = function () {
    return this.getAttribute("form"); 
  };
  for(var i = 0; i < items.length; i++) {
  var nameForm = items[i].getAttribute("form");
    var subs = items[i].children;
    for(var j = 0; j < subs.length; j++) {
      if(subs[j].tagName == 'BUTTON' || subs[j].tagName == 'INPUT' || subs[j].tagName == 'FIELDSET' || subs[j].tagName == 'OUTPUT' || subs[j].tagName == 'SELECT' || subs[j].tagName == 'TEXTAREA') { 
        subs[j].setAttribute("form",nameForm);
      }
    }
    Object.defineProperty(items[i], 'form', {
      get: getter
    });
  }
}
window.addEventListener("load",control,true);

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

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