本文翻译自:Is it valid to have a html form inside another html form?
Is it valid html to have the following: 是否具有以下内容是有效的html:
<form action="a">
<input.../>
<form action="b">
<input.../>
<input.../>
<input.../>
</form>
<input.../>
</form>
So when you submit "b" you only get the fields within the inner form. 因此,当您提交“ b”时,您只会在内部表单中获得字段。 When you submit "a" you get all fields minus those within "b". 提交“ a”时,您将获得减去“ b”中所有字段的所有字段。
If it isn't possible, what workarounds for this situation are available? 如果不可能,那么针对这种情况有哪些解决方法可用?
#1楼
参考:https://stackoom.com/question/2Kca/在另一个html表单中包含html表单是否有效
#2楼
In case someone find this post here is a great solution without the need of JS. 万一有人发现这篇文章,这是不需要JS的绝佳解决方案。 Use two submit buttons with different name attributes check in your server language which submit button was pressed cause only one of them will be sent to the server. 使用两个具有不同名称属性的提交按钮,以您的服务器语言检查被按下的提交按钮,因为只有其中一个被发送到服务器。
<form method="post" action="ServerFileToExecute.php">
<input type="submit" name="save" value="Click here to save" />
<input type="submit" name="delete" value="Click here to delete" />
</form>
The server side could look something like this if you use php: 如果使用php,服务器端可能看起来像这样:
<?php
if(isset($_POST['save']))
echo "Stored!";
else if(isset($_POST['delete']))
echo "Deleted!";
else
echo "Action is missing!";
?>
#3楼
如果您需要表单将数据提交/提交到1:M关系数据库,我建议在表A上创建一个“插入后”数据库触发器,该触发器将为表B插入必要的数据。
#4楼
HTML 4.x & HTML5 disallow nested forms, but HTML5 will allow a workaround with "form" attribute ("form owner"). HTML 4.x和HTML5不允许使用嵌套表单,但是HTML5允许使用“表单”属性(“表单所有者”)进行变通。
As for HTML 4.x you can: 至于HTML 4.x,您可以:
- Use an extra form(s) with only hidden fields & JavaScript to set its input's and submit the form. 使用仅包含隐藏字段和JavaScript的额外表单来设置其输入内容并提交表单。
- Use CSS to line up several HTML form to look like a single entity - but I think that's too hard. 使用CSS排列几种HTML表单,使其看起来像一个实体-但这太难了。
#5楼
No, it is not valid. 不,这是无效的。 But a "solution" can be creating a modal window outside of form "a" containing the form "b". 但是,“解决方案”可以在包含形式“ b”的形式“ a”之外创建模式窗口。
<div id="myModalFormB" class="modal">
<form action="b">
<input.../>
<input.../>
<input.../>
<button type="submit">Save</button>
</form>
</div>
<form action="a">
<input.../>
<a href="#myModalFormB">Open modal b </a>
<input.../>
</form>
It can be easily done if you are using bootstrap or materialize css. 如果您使用引导程序或实例化CSS,则可以轻松完成。 I'm doing this to avoid using iframe. 我这样做是为了避免使用iframe。
#6楼
不可以,HTML规范指出任何FORM
元素都不应包含另一个FORM
元素。