可以像这样嵌套HTML表单吗?
<form name="mainForm">
<form name="subForm">
</form>
</form>
这样两种形式都能工作吗?我的朋友在这方面遇到了问题,
subForm
的一部分可以工作,而另一部分却不能。可以像这样嵌套HTML表单吗?
<form name="mainForm">
<form name="subForm">
</form>
</form>
subForm
的一部分可以工作,而另一部分却不能。从HTML5工作草案中:
4.10.3
form
元素内容模型:
流内容,但没有表单元素的后代。
HTML5的<input>
标签的form
属性可以解决问题。
来自http://www.w3schools.com/tags/att_input_form.asp:
form
属性是HTML5中的新属性。- 指定一个
<input>
元素属于哪个<form>
元素。该属性的值必须为同一文档中<form>
元素的id
属性。
场景:
实现:
<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" />
在这里您可以找到浏览器的兼容性。
<input type="submit"
与指定了action
的<form>
相连,那么与该<form>
相关联的<input />
将会在请求中被使用。 - Cliff Burton可以实现与嵌套表单相同的结果,但不需要将它们嵌套。
HTML5引入了form属性。您可以向表单控件添加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规范:
该功能允许作者绕过对嵌套表单元素的支持不足。
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;
}
纯HTML不能让你做到这一点。但是通过JavaScript,您可以实现这一点。 如果您正在使用JavaScript/jQuery,您可以使用类对表单元素进行分类,然后使用serialize()仅序列化您要提交的子集项中的那些表单元素。
<form id="formid">
<input type="text" class="class1" />
<input type="text" class="class2">
</form>
然后在你的JavaScript中,你可以这样做来序列化class1元素
$(".class1").serialize();
对于class2,您可以这样做:
$(".class2").serialize();
整个表单$("#formid").serialize();
或者简单地说
$("#formid").submit();
ng-app
中的任何<form>
标签都会在运行时被替换为ngForm
指令,这些指令旨在嵌套使用。<form>
元素,因此Angular提供了ngForm
指令,其行为与<form>
相同,但可以嵌套。这使您可以拥有嵌套表单,在使用动态生成的表单时非常有用,使用ngRepeat
指令和Angular验证指令。(来源:source)如果您正在使用一些服务器端脚本语言来操作提交的数据,那么解决这个问题的另一种方法是像这样声明您的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仅包含您的“主表单”数据,您的子表单数据存储在另一个变量中,您可以随意操作。
希望这可以帮助您!
如 Craig 所说,不。
但是,关于你提到的原因:
使用一个带有输入和“更新”按钮的 <form>
,并在另一个 <form>
中使用复制隐藏输入和“提交订单”按钮可能会更容易些。
<button type="submit" formaction="/rmDog" method='post' id="rmDog">-</button>
这将作为单独的按钮嵌套在原始表单中。
formmethod
属性 — https://www.w3schools.com/tags/att_button_formmethod.asp - Inversion