提交按钮是否必须包含在表单中

Rod*_*Rod 8 html javascript

我希望我的提交按钮位于我的表单元素之外的某个位置?我有什么选择吗?除了jquery.

谢谢,rodchar

Sam*_*son 5

另一种方法是仅在按钮上设置form属性

<form id="first">
    <input type="submit" form="second" value="Submit Second" />
</form>
<form id="second">
    <input type="submit" form="first" value="Submit First" />
</form>
Run Code Online (Sandbox Code Playgroud)

小提琴:http : //jsfiddle.net/52wgc2ym/


原始答案

提交按钮的自然行为是在其层次结构中提交最接近的表单。获取按钮提交不存在的表单的唯一其他方法是使用JavaScript(基本上就是jQuery)。

如有必要,您可以重新定位“提交”按钮,以便呈现页面时看起来好像不在窗体中。您可以使用CSS进行此操作,这可能会提供所需的结果。


egr*_*nin 2

这是一种常见的情况。我认为这可以做到(尚未测试):

<form id="form1" action="someAction.cgi" method="GET">
    <!-- other fields go here -->
</form>
<form id="form2" action="someOtherAction.cgi" method="GET">
    <!-- other fields go here -->
</form>

<form>
    <input value="Form One" type="button"
        onclick="document.getElementById('form1').submit();"/>
    <input value="Form Two" type="button"
        onclick="document.getElementById('form2').submit();"/>
</form>
Run Code Online (Sandbox Code Playgroud)

我不确定你是否需要最后一个<form>。我似乎记得如果按钮不在form.