如何使用javascript提交表单?

时间:2012-03-24 21:10:59

标签: javascript

我有一个标识为theForm的表单,其中包含以下div,其中包含一个提交按钮:

<div id="placeOrder" 
     style="text-align: right; width: 100%; background-color: white;">
     <button type="submit" 
             class='input_submit' 
             style="margin-right: 15px;" 
             onClick="placeOrder()">Place Order
     </button>
</div>

单击时,将调用函数placeOrder()。该函数将上面div的innerHTML更改为“processing ...”(因此提交按钮现在消失了)。

以上代码有效,但现在的问题是我无法提交表单!我已经尝试将其放在placeOrder()函数中:

document.theForm.submit();

但这不起作用。

如何才能提交表单?

10 个答案:

答案 0 :(得分:114)

将表单的name属性设置为"theForm",您的代码就可以使用。

答案 1 :(得分:85)

你可以使用......

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

...但替换innerHTML。您可以隐藏表单,然后在其位置插入处理... span

var form = document.getElementById('theForm');

form.style.display = 'none';

var processing = document.createElement('span');

processing.appendChild(document.createTextNode('processing ...'));

form.parentNode.insertBefore(processing, form);

答案 2 :(得分:28)

document.getElementById("form1").submit();

在我的案例中它完美无缺。

你也可以在功能上使用它 等,

function formSubmit()
{
     document.getElementById("form1").submit();
} 

答案 3 :(得分:10)

document.forms["name of your form"].submit();

document.getElementById("form id").submit();

你可以尝试任何这个..这肯定会有效..

答案 4 :(得分:3)

//Take form name as "theForm"
//Example:   
         ## ----Html code----- ##    
      <form method="post" action="" name="theForm">
            <button onclick="placeOrder()">Place Order</button>
      </form>

            ## -------javascript code------ ##

        function placeOrder(){
        document.theForm.action="yourUrl";
        document.theForm.submit();
        }

答案 5 :(得分:3)

这是一篇旧帖子,但我会按照提交表单的方式离开,而不在表单中使用name标记:

<强> HTML

<button type="submit" onClick="placeOrder(this.form)">Place Order</button>

<强> JS

function placeOrder(form){
    form.submit();
}

希望这个解决方案可以帮助其他人。 TQ

答案 6 :(得分:2)

如果您的表单没有任何ID,但它的类名称类似于theForm,您可以使用以下语句提交它:

document.getElementsByClassName("theForm")[0].submit();

答案 7 :(得分:0)

我想出了一个简单的解决方案,使用隐藏在我网站上的简单表单,其中包含用户登录的相同信息。示例如果您希望用户登录此表单,您可以将以下内容添加到下面的表单中。

<input type="checkbox" name="autologin" id="autologin" />

据我所知,我是第一个隐藏表单并通过点击链接提交表单的人。有链接提交隐藏的表格和信息。如果您不喜欢自己的网站上的自动登录方法,并且密码位于隐藏的表单密码文本区域,则不是100%安全......

好的,这是工作。假设$siteid是帐户,$sitepw是密码。

首先在php脚本中创建表单,如果你不喜欢html,那么使用最小数据然后以隐藏形式回显值。我只是使用php值和echo在我想要的表格按钮旁边的任何地方回显,因为你看不到它。

要打印的PHP表单

$hidden_forum = '
<form id="alt_forum_login" action="./forum/ucp.php?mode=login" method="post" style="display:none;">
    <input type="text" name="username" id="username" value="'.strtolower($siteid).'" title="Username" />
    <input type="password" name="password" id="password" value="'.$sitepw.'" title="Password" />
</form>';

PHP和链接提交表单

<?php print $hidden_forum; ?>
<pre><a href="#forum" onClick="javascript: document.getElementById('alt_forum_login').submit();">Forum</a></pre>

答案 8 :(得分:0)

您可以使用以下代码使用Javascript提交表单;

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

答案 9 :(得分:-1)

<html>
<body>

<p>Enter some text in the fields below, then press the "Submit form" button to submit the form.</p>

<form id="myForm" action="/action_page.php">
  First name: <input type="text" name="fname"><br>
  Last name: <input type="text" name="lname"><br><br>
  <input type="button" onclick="myFunction()" value="Submit form">
</form>

<script>
function myFunction() {
    document.getElementById("myForm").submit();
}
</script>

</body>
</html>