是否可以像这样嵌套html表单
<form name="mainForm">
<form name="subForm">
</form>
</form>
两种形式都适用吗?我的朋友有这个问题,subForm的一部分工作,而另一部分不工作。
是否可以像这样嵌套html表单
<form name="mainForm">
<form name="subForm">
</form>
</form>
两种形式都适用吗?我的朋友有这个问题,subForm的一部分工作,而另一部分不工作。
当前回答
虽然这个问题已经很老了,而且我同意@everyone的观点,HTML中不允许嵌套表单
但这个东西大家可能都想看看
在那里你可以hack(我称之为黑客,因为我确定这不是合法的)html允许浏览器有嵌套的形式
<form id="form_one" action="http://apple.com">
<div>
<div>
<form id="form_two" action="/">
<!-- DUMMY FORM TO ALLOW BROWSER TO ACCEPT NESTED FORM -->
</form>
</div>
<br/>
<div>
<form id="form_three" action="http://www.linuxtopia.org/">
<input type='submit' value='LINUX TOPIA'/>
</form>
</div>
<br/>
<div>
<form id="form_four" action="http://bing.com">
<input type='submit' value='BING'/>
</form>
</div>
<br/>
<input type='submit' value='Apple'/>
</div>
</form>
Js小提琴link
http://jsfiddle.net/nzkEw/10/
其他回答
注意,不允许嵌套FORM元素!
http://www.w3.org/MarkUp/html3/forms.html
https://www.w3.org/TR/html4/appendix/changes.html#h-A.3.9 (html4规范指出从3.2到4的嵌套表单没有变化)
https://www.w3.org/TR/html4/appendix/changes.html#h-A.1.1.12 (html4规范指出,从4.0到4.1,嵌套表单没有变化)
https://www.w3.org/TR/html5-diff/ (html5规范指出从4到5的嵌套表单没有变化)
https://www.w3.org/TR/html5/forms.html#association-of-controls-and-forms评论到“此功能允许作者在缺乏对嵌套表单元素的支持的情况下工作”,但没有引用这是在哪里指定的,我认为他们是在假设我们应该假设它是在html3规范中指定的:)
HTML5 <input>表单属性可以是解决方案。 从http://www.w3schools.com/tags/att_input_form.asp:
表单属性是HTML5中的新属性。 指定<input>元素属于哪个<form>元素。此属性的值必须是同一文档中<form>元素的id属性。
场景:
input_Form1_n1 input_Form2_n1 input_Form1_n2 input_Form2_n2
实现:
<form id="Form1" action="Action1.php" method="post"></form>
<form id="Form2" action="Action2.php" method="post"></form>
<input type="text" name="input_Form1_n1" form="Form1" />
<input type="text" name="input_Form2_n1" form="Form2" />
<input type="text" name="input_Form1_n2" form="Form1" />
<input type="text" name="input_Form2_n2" form="Form2" />
<input type="submit" name="button1" value="buttonVal1" form="Form1" />
<input type="submit" name="button2" value="buttonVal2" form="Form2" />
在这里你可以找到浏览器的兼容性。
正如克雷格所说,不。
但是,关于你所说的原因:
使用1 <form>的输入和“Update”按钮,并在另一个<form>的“Submit Order”按钮中使用复制隐藏输入可能会更容易。
虽然这个问题已经很老了,而且我同意@everyone的观点,HTML中不允许嵌套表单
但这个东西大家可能都想看看
在那里你可以hack(我称之为黑客,因为我确定这不是合法的)html允许浏览器有嵌套的形式
<form id="form_one" action="http://apple.com">
<div>
<div>
<form id="form_two" action="/">
<!-- DUMMY FORM TO ALLOW BROWSER TO ACCEPT NESTED FORM -->
</form>
</div>
<br/>
<div>
<form id="form_three" action="http://www.linuxtopia.org/">
<input type='submit' value='LINUX TOPIA'/>
</form>
</div>
<br/>
<div>
<form id="form_four" action="http://bing.com">
<input type='submit' value='BING'/>
</form>
</div>
<br/>
<input type='submit' value='Apple'/>
</div>
</form>
Js小提琴link
http://jsfiddle.net/nzkEw/10/
在我知道我不应该这样做之前,我已经为拥有多个提交按钮而嵌套了表单。这样运行了18个月,数千个注册交易,没有人打电话给我们任何困难。
嵌套表单为我提供了一个ID来解析要采取的正确操作。直到我试图将一个字段附加到其中一个按钮和验证抱怨,才打破。不是一个大问题来解决它-我使用了一个显式的stringify在外部的形式,所以它没有关系提交和形式不匹配。是的,是的,应该把按钮从submit变成onclick。
关键是在某些情况下,它并没有完全被破坏。但“没有完全崩溃”可能是一个太低的标准:-)