尝试使用onclick事件提交父表单,而不是工作

时间:2013-11-21 01:05:15

标签: javascript javascript-events

我正在尝试使用“onclick”事件提交父表单,而不指定表单的ID。有一个原因。我使用webfont作为按钮,因此是“span”。

我的代码:

<form action="/Index" method="post">    <p>
    <span class="fa fa-arrow-circle-right" onclick="document.form.submit();" style="font-size:50px;cursor:pointer;"></span>  
    <input type="hidden" name="id" value="10000"/>     
</p>

我也尝试过:

onclick="this.form.submit();"

这也无效。

请使用正确的代码&gt;

提前致谢。

4 个答案:

答案 0 :(得分:1)

如果页面中只有一个表单,则可以使用document.forms[0].submit()

答案 1 :(得分:0)

还有其他形式吗?

如果不是,请尝试:'document.forms [0] .submit'

否则,在表单中添加id

<form id="testForm" action='..'>...</form>

document.getElementById('testForm').submit();

答案 2 :(得分:0)

您没有关闭表单元素。

<form action="/Index" method="post">    
    <p>
    <span class="fa fa-arrow-circle-right" onclick="document.form.submit();" style="font-size:50px;cursor:pointer;"></span>  
    <input type="hidden" name="id" value="10000"/>     
    </p>
</form> /*This*/

关闭后应该可以使用。

答案 3 :(得分:0)

即使您在同一页面上有多个表单,只要您为每个表单提供唯一的ID,以下提交表单的策略也会有效。应该为<form>提供一个id属性,以便可以使用给定的id定位特定对象,如下所示:

<form id="myFormId" action="/Index" method="post">
    <p>
    <span onclick="document.getElementById('myFormId').submit()" class="fa fa-arrow-circle-right" role="button" style="font-size:50px;cursor:pointer;"></span>  
    <input type="hidden" name="id" value="10000"/>     
    </p>
</form>
相关问题