Html 表格里面的表格,可以吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3430214/
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
Form inside a form, is that alright?
提问by Rajasekar
Whether we can have a form inside another form?. Is there any problem with that.
我们是否可以在另一个表单中包含一个表单?这有什么问题吗。
回答by Oded
Though you can have several <form>
elements in one HTML page, you cannot nest them.
尽管<form>
在一个 HTML 页面中可以有多个元素,但不能嵌套它们。
回答by ilevent
Form nesting can be achieved with new HTML5 input element's form attribute. Although we don't nest forms structurally, inputs are evaluated as they are in their own form. In my tests, 3 major browsers support this except IE(IE11). Form nesting limitation was a big obstacle for HTML UI design.
可以使用新的 HTML5 input 元素的 form 属性来实现表单嵌套。虽然我们没有在结构上嵌套表单,但输入会按照它们自己的形式进行评估。在我的测试中,除 IE(IE11) 外,3 个主要浏览器都支持此功能。表单嵌套限制是 HTML UI 设计的一大障碍。
Here is a sample code, when you click Save button you should see "2 3 success" (Original http://www.impressivewebs.com/html5-form-attribute/):
这是一个示例代码,当您单击“保存”按钮时,您应该看到“2 3 成功”(原文http://www.impressivewebs.com/html5-form-attribute/):
<form id="saveForm" action="/post/dispatch/save" method="post"></form>
<form id="deleteForm" action="/post/dispatch/delete" method="post"></form>
<div id="toolbar">
<input type="text" name="foo" form="saveForm" />
<input type="hidden" value="some_id" form="deleteForm" />
<input type="text" name="foo2" id="foo2" form="saveForm" value="success" />
<input type="submit" name="save" value="Save" form="saveForm" onclick="alert(document.getElementById('deleteForm').elements.length + ' ' + document.getElementById('saveForm').elements.length + ' ' + document.getElementById('saveForm').elements['foo2'].value);return false;" />
<input type="submit" name="delete" value="Delete" form="deleteForm" />
<a href="/home/index">Cancel</a>
</div>
回答by Quentin
No. HTML explicitly forbids nested forms.
不可以。HTML 明确禁止嵌套表单。
From the HTML 5 draft:
Content model: Flow content, but with no form element descendants.
内容模型:流内容,但没有表单元素后代。
From the HTML 4.01 Recommendation:
来自HTML 4.01 建议:
<!ELEMENT FORM - - (%block;|SCRIPT)+ -(FORM) -- interactive form -->
<!ELEMENT FORM - - (%block;|SCRIPT)+ -(FORM) -- interactive form -->
(Note the -(FORM) section).
(注意 -(FORM) 部分)。
回答by user3007766
Nested forms are not supported and are not part of the w3c standard ( as many of you have stated ).
嵌套表单不受支持,也不是 w3c 标准的一部分(正如你们中的许多人所说)。
However HTML5 adds support for inputs that don't have to be descendants of any form, but can be submitted in several forms - by using the "form" attribute. This doesn't exactly allow nested forms, but by using this method, you can simulate nested forms.
然而,HTML5 增加了对不必是任何表单的后代但可以以多种表单提交的输入的支持 - 通过使用“表单”属性。这并不完全允许嵌套表单,但通过使用此方法,您可以模拟嵌套表单。
The value of the "form" attribute must be id of the form, or in case of multiple forms, separate form id's with space.
"form" 属性的值必须是表单的 id,或者在多个表单的情况下,用空格分隔表单 id。
You can read more here
你可以在这里阅读更多
回答by Ben Call
If you have a master form and are forced to have a "form with a form" Here is what you can do... in my case I had a link in the globalHeader and I wanted to perform a post when it was clicked:
如果您有一个主表单并且被迫拥有一个“带有表单的表单”,那么您可以这样做......在我的情况下,我在 globalHeader 中有一个链接,我想在点击它时执行一个帖子:
Example form post with link button submit:
带有链接按钮提交的示例表单帖子:
Instead of a form... wrap your input in a div:
而不是表单...将您的输入包装在一个 div 中:
<div id="gap_form"><input type="hidden" name="PostVar"/><a id="myLink" href="javascript:Form2.submit()">A Link</a></div>
js file:
js文件:
$(document).ready(function () {
(function () {
$('#gap_form').wrap('<form id="Form2" action="http://sitetopostto.com/postpage" method="post" target="_blank"></form>');
})();});
This would wrap everything inside the div "gap_form" inside a form on the fly and the link would submit that form. I have this exact example working on a page now... (In my example...You could accomplish the same thing by redirecting to a new page and submitting the form on that page... but I like this better)
这会将 div "gap_form" 中的所有内容动态包装在一个表单中,并且该链接将提交该表单。我现在在一个页面上有这个确切的例子......(在我的例子中......你可以通过重定向到一个新页面并在该页面上提交表单来完成同样的事情......但我更喜欢这个)
回答by manuchap
Another workaround would be to initiate a modal window containing its own form
另一种解决方法是启动一个包含自己表单的模式窗口
回答by AlexanderMP
Yes there is. It is wrong. It won't work because it is wrong. Most browsers will only see one form.
就在这里。这是错误的。它不会工作,因为它是错误的。大多数浏览器只会看到一种形式。
回答by Martin LeBlanc
It's not valid XHTML to have to have nested forms. However, you can use multiple submit buttons and use a serverside script to run different codes depending on which button the users has clicked.
必须具有嵌套表单是无效的 XHTML。但是,您可以使用多个提交按钮并使用服务器端脚本根据用户单击的按钮运行不同的代码。
回答by Gautam3164
No we can't nest forms within another form like this
不,我们不能在这样的另一种形式中嵌套形式
<form name='form1'>
<form name='form2'>
//some code here
</form>
</form>
it will work in some cases but it is not recommended for universal platforms. You can use plenty of SUBMIT buttons inside a single form, but you can't manage a nested form appropriately.
它在某些情况下可以工作,但不建议用于通用平台。您可以在单个表单中使用大量 SUBMIT 按钮,但您无法适当地管理嵌套表单。