表单外部的 HTML 输入字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6644128/
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
HTML input field OUTSIDE of a form
提问by dlitwak
I have a form with a set of inputs, and I want my page to refresh when one of them changes. I have a second set of inputs on the OTHER side of the page, and the css layout doesn't make it convenient for me to put them in the same <form>
</form>
tag. I was wondering if there is a way that I can make sure those "inputs" that are located outside of the <form>
tag are still associated with that form.
我有一个包含一组输入的表单,我希望我的页面在其中一个更改时刷新。我在页面的另一侧有第二组输入,并且 css 布局不方便我将它们放在同一个<form>
</form>
标签中。我想知道是否有一种方法可以确保位于<form>
标签外部的那些“输入”仍然与该表单相关联。
Is there some way we can assign a "form id" to the inputs?
有什么方法可以为输入分配“表单ID”?
回答by You
In HTML5, you can use the form
attribute:
在 HTML5 中,您可以使用form
属性:
A form-associated element is, by default, associated with its ancestor form element, but may have a form attribute specified to override this.
If a form-associated element has a form attribute specified, then that attribute's value must be the ID of a form element in the element's owner Document.
默认情况下,与表单相关的元素与其祖先表单元素相关联,但可以指定一个表单属性来覆盖它。
如果与表单关联的元素指定了表单属性,则该属性的值必须是元素所有者文档中表单元素的 ID。
Example:
例子:
<form id="myform">
<input id="something" type="text">
</form>
<button form="myform" type="submit">Submit that form over there</button>
You should however make sure that it is clear to the user that these visually separated form elements are in fact connected.
但是,您应该确保用户清楚这些视觉上分离的表单元素实际上是相互连接的。
回答by eaglei22
<input type="text" form="myform" />
worked for me.
<input type="text" form="myform" />
为我工作。
Update
This worked great with FireFox, however gave me trouble in IE 11 as the form attribute is not recognized with IE (as of writing this).
更新
这在 FireFox 中效果很好,但是在 IE 11 中给我带来了麻烦,因为 IE 无法识别表单属性(在撰写本文时)。
I had to just set a hidden input field inside the form, and transferred value to hidden input field from input outside form; with onclick using jQuery.
我只需要在表单内部设置一个隐藏的输入字段,然后将值从表单外部的输入转移到隐藏的输入字段;使用 jQuery 的 onclick。