简体   繁体   English

HTML 字段集:表单属性不起作用

[英]HTML fieldset: form attribute not working

I am trying to create an HTML form is separate parts for layout reasons.由于布局原因,我正在尝试创建一个 HTML 表单是单独的部分。 As far as I understand, you can use a fieldset with a form attribute to associate the fieldset with the form, even if it's not inside the form ( https://developer.mozilla.org/en-US/docs/Web/HTML/Element/fieldset ).据我了解,您可以使用带有form属性的字段集将fieldset集与表单相关联,即使它不在表单内( fieldset ://developer.mozilla.org/en-US/docs/Web/HTML /元素/字段集)。

However, if I have a separate fieldset with a submit button or another input in it, it doesn't seem to work.但是,如果我有一个带有提交按钮或其他输入的单独字段集,它似乎不起作用。

 <form id="test"> <input name="inside-stuff" value="Inside"> <button type="submit">Doit Inside</button> </form> <fieldset form="test"> <input name="outside-stuff" value="Outside"> <button type="submit">Doit Outside</button> </fieldset>

In the above snippet I have two submit buttons and two inputs.在上面的代码片段中,我有两个提交按钮和两个输入。 The one in the actual form works, while the one in the attached fieldset doesn't.实际表单中的那个有效,而附加fieldset集中的那个无效。 When I use the inside submit button, it only submits what's in side the main form, not what is in the associated fieldset .当我使用内部提交按钮时,它只提交主表单中的内容,而不是关联的fieldset中的内容。

This may not be obvious when running the snippet, but is certainly the case when tried in real life.这在运行代码片段时可能并不明显,但在现实生活中尝试时确实如此。

What is missing to make this work?使这项工作缺少什么?

Update 1更新 1

The problem appears to be more generic than that.这个问题似乎比这更普遍。 I find that input elements inside an associated fieldset don't get submitted either.我发现关联字段集中的输入元素也没有被提交。

Update 2更新 2

This is not a duplicate of Submit form using a button outside the <form> tag .不是使用 <form> 标签外的按钮提交表单的副本。 This question specifically refers to a fieldset element.这个问题专门指一个 fieldset 元素。 The other doesn't even mention it.另一个甚至没有提到它。

I wrote the following javascript我写了以下 javascript

function control() {
  function view(i) {
    var frm = items[i].getAttribute("form");
    var fBase = document.querySelector("form[id=" + frm + "]");
    fBase.addEventListener("submit",  function(){
      var fld = document.querySelector("fieldset[form='" + this.id + "']");
      var cln = fld.cloneNode(true);
      cln.style.display = "none";
      document.getElementById(frm).appendChild(cln);         
    },true);
  }

  var items = document.querySelectorAll("FIELDSET[form]");
  
  var getter = function () {
    return this.getAttribute("form"); 
  };

  for(var i = 0; i < items.length; i++) {
    view(i);
    Object.defineProperty(items[i], 'form', {
      get: getter
    });
  }
}
window.addEventListener("load",control,true);

It's happening because the Form you have placed inside the fieldset is wrong.发生这种情况是因为您放置在字段集中的表单是错误的。 The form should be the parent of the fieldset in order to get it to work.表单应该是字段集的父级才能使其正常工作。 The form tag should always be the parent of the fieldset.表单标记应始终是字段集的父级。

If you place <form> and <fieldset> then it will work.如果你放置<form><fieldset>那么它会起作用。 The code below should do.下面的代码应该可以。

<form id="test">
   <input name="stuff">
   <button type="submit">Doit</button>
</form>
<form>
   <fieldset>
      <input type="text" name="stuff2">
      <button type="submit">Doit</button>
   </fieldset>
</form>

I hope this will help!我希望这个能帮上忙!

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

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