是否可以像这样嵌套html表单

<form name="mainForm">
  <form name="subForm">
  </form>
</form>

两种形式都适用吗?我的朋友有这个问题,subForm的一部分工作,而另一部分不工作。


当前回答

今天,我也遇到了同样的问题,为了解决这个问题我增加了一个用户控件和 在这个控件上,我使用这个代码

<div class="divformTagEx">

</div>

<asp:Literal runat="server" ID="litFormTag" Visible="false">
'<div> <form  style="margin-bottom: 3;" action="http://login.php" method="post" name="testformtag"></form> </div>'</asp:Literal>

并在页面的PreRenderComplete事件中调用此方法

private void InitializeJavaScript()
{
        var script = new StringBuilder();
        script.Append("$(document).ready(function () {");
        script.Append("$('.divformTagEx').append( ");
        script.Append(litFormTag.Text);
        script.Append(" )");
        script.Append(" });");
        ScriptManager.RegisterStartupScript(this, GetType(), "nestedFormTagEx", script.ToString(), true);
    }

我相信这会有帮助。

其他回答

另一种解决这个问题的方法是,如果你使用一些服务器端脚本语言,允许你操纵发布的数据,就是这样声明你的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现在只有你的“主表单”数据,你的子表单数据存储在你可以随意操作的另一个变量中。

希望这能有所帮助!

注意,不允许嵌套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工作草案:

4.10.3 form元素 内容模型: 流内容,但没有表单元素后代。

关于嵌套表单:我花了10年的时间调试一个ajax脚本。

对不起,我之前的回答/例子没有说明HTML标记。

<form id='form_1' et al>
  <input stuff>
  <submit onClick='ajaxFunction(That_Puts_form_2_In_The_ajaxContainer)'>
  <td id='ajaxContainer'></td>
</form>

Form_2不断失败,表示无效的Form_2。

当我把产生form_2 < I >的ajaxContainer移到form_1的</ I >外时,我又回到了业务中。它回答了为什么会嵌套形式的问题。我的意思是,如果不定义要使用哪种表单,ID是什么?肯定有更好、更圆滑的办法。

正如克雷格所说,不。

但是,关于你所说的原因:

使用1 <form>的输入和“Update”按钮,并在另一个<form>的“Submit Order”按钮中使用复制隐藏输入可能会更容易。