是否可以像这样嵌套html表单
<form name="mainForm">
<form name="subForm">
</form>
</form>
两种形式都适用吗?我的朋友有这个问题,subForm的一部分工作,而另一部分不工作。
是否可以像这样嵌套html表单
<form name="mainForm">
<form name="subForm">
</form>
</form>
两种形式都适用吗?我的朋友有这个问题,subForm的一部分工作,而另一部分不工作。
当前回答
在我知道我不应该这样做之前,我已经为拥有多个提交按钮而嵌套了表单。这样运行了18个月,数千个注册交易,没有人打电话给我们任何困难。
嵌套表单为我提供了一个ID来解析要采取的正确操作。直到我试图将一个字段附加到其中一个按钮和验证抱怨,才打破。不是一个大问题来解决它-我使用了一个显式的stringify在外部的形式,所以它没有关系提交和形式不匹配。是的,是的,应该把按钮从submit变成onclick。
关键是在某些情况下,它并没有完全被破坏。但“没有完全崩溃”可能是一个太低的标准:-)
其他回答
虽然我没有给出嵌套表单的解决方案(它不可靠地工作),但我确实提出了一个适合我的解决方案:
使用场景:允许一次更改N个项的超表单。它底部有一个“提交全部”按钮。每个项目都有自己的嵌套表单,带有“提交项目# N”按钮。但是不能…
在这种情况下,实际上可以使用单个表单,然后将按钮的名称设为submit_1..submit_N和submitAll并在服务器端处理它,如果按钮的名称是submit_1,则只查看以_1结尾的参数。
<form>
<div id="item1">
<input type="text" name="foo_1" value="23">
<input type="submit" name="submit_1" value="Submit Item #1">
</div>
<div id="item2">
<input type="text" name="foo_2" value="33">
<input type="submit" name="submit_2" value="Submit Item #2">
</div>
<input type="submit" name="submitAll" value="Submit All Items">
</form>
好吧,这算不上什么发明,但确实管用。
第二个表单将被忽略,参见WebKit的代码片段:
bool HTMLParser::formCreateErrorCheck(Token* t, RefPtr<Node>& result)
{
// Only create a new form if we're not already inside one.
// This is consistent with other browsers' behavior.
if (!m_currentFormElement) {
m_currentFormElement = new HTMLFormElement(formTag, m_document);
result = m_currentFormElement;
pCloserCreateErrorCheck(t, result);
}
return false;
}
可以实现与嵌套表单相同的结果,但不需要嵌套它们。
HTML5引入了表单属性。可以将form属性添加到窗体外部的窗体控件,以便将它们链接到特定的窗体元素(通过id)。
https://www.impressivewebs.com/html5-form-attribute/
这样你可以像这样构造你的html:
<form id="main-form" action="/main-action" method="post"></form>
<form id="sub-form" action="/sub-action" method="post"></form>
<div class="main-component">
<input type="text" name="main-property1" form="main-form" />
<input type="text" name="main-property2" form="main-form" />
<div class="sub-component">
<input type="text" name="sub-property1" form="sub-form" />
<input type="text" name="sub-property2" form="sub-form" />
<input type="submit" name="sub-save" value="Save" form="sub-form" />
</div>
<input type="submit" name="main-save" value="Save" form="main-form" />
</div>
所有现代浏览器都支持form属性。虽然IE不支持这个功能,但IE不再是浏览器,而是一个兼容工具,微软自己也证实了这一点:https://www.zdnet.com/article/microsoft-security-chief-ie-is-not-a-browser-so-stop-using-it-as-your-default/。是时候停止关心如何在IE中工作了。
https://caniuse.com/#feat=form-attribute https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fae-form
来自html规范:
该特性允许作者解决缺乏支持的问题 嵌套表单元素。
注意,不允许嵌套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规范中指定的:)
另一种解决这个问题的方法是,如果你使用一些服务器端脚本语言,允许你操纵发布的数据,就是这样声明你的html表单:
<form>
<input name="a_name"/>
<input name="a_second_name"/>
<input name="subform[another_name]"/>
<input name="subform[another_second_name]"/>
</form>
如果你打印发布的数据(我将在这里使用PHP),你会得到一个这样的数组:
//print_r($_POST) will output :
array(
'a_name' => 'a_name_value',
'a_second_name' => 'a_second_name_value',
'subform' => array(
'another_name' => 'a_name_value',
'another_second_name' => 'another_second_name_value',
),
);
然后你可以这样做:
$my_sub_form_data = $_POST['subform'];
unset($_POST['subform']);
你的$_POST现在只有你的“主表单”数据,你的子表单数据存储在你可以随意操作的另一个变量中。
希望这能有所帮助!