提问人:Nate 提问时间:3/25/2012 最后编辑:Peter MortensenNate 更新时间:8/27/2023 访问量:1003482
如何使用 JavaScript 提交表单?
How can I submit a form using JavaScript?
问:
我有一个带有 id 的表单,其中包含以下带有提交按钮的 div:theForm
<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>
单击时,将调用该函数。该函数将上述 div 的 innerHTML 更改为 “processing ...”(所以提交按钮现在不见了)。placeOrder()
上面的代码有效,但现在的问题是我无法提交表单!我试过把它放在函数中:placeOrder()
document.theForm.submit();
但这行不通。
我怎样才能提交表格?
答:
您可以使用...
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);
评论
action="/{controller}/{action-method}" method="post"
document.getElementById('printoffersForm').submit();
将窗体的属性设置为,代码将起作用。name
"theForm"
评论
document.theForm.submit()
在我的情况下,它运行良好。
document.getElementById("form1").submit();
此外,您可以在以下函数中使用它:
function formSubmit()
{
document.getElementById("form1").submit();
}
评论
input
document.forms["name of your form"].submit();
或
document.getElementById("form id").submit();
你可以试试这些......这肯定会起作用......
评论
如果您的表单没有任何 ID,但它具有类似 theForm 的类名,则可以使用以下语句提交它:
document.getElementsByClassName("theForm")[0].submit();
评论
我提出了一个简单的解决方案,使用隐藏在我的网站上的简单表格,其中包含用户登录时使用的信息。示例:如果您希望用户在此表单上登录,则可以在下面的表单中添加类似以下内容的内容。
<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>
评论
[HTML全文]
<!-- change id attribute to name -->
<form method="post" action="yourUrl" name="theForm">
<button onclick="placeOrder()">Place Order</button>
</form>
JavaScript的
function placeOrder () {
document.theForm.submit()
}
评论
我将保留提交表单的方式,而不使用表单内的标签:name
[HTML全文]
<button type="submit" onClick="placeOrder(this.form)">Place Order</button>
JavaScript的
function placeOrder(form){
form.submit();
}
您可以使用以下代码通过 JavaScript 提交表单:
document.getElementById('FormID').submit();
<html>
<body>
<p>Enter some text in the fields below, and 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>
评论
如果您在这里寻找使用表单外部按钮提交表单的最佳方式:
从 HTML5 开始,只需使用按钮的属性。form
<button type="submit" form="my_form_id">Submit</button>
此方法可以在 JS 内联 onClicks 无法处理的独特上下文(例如:Stripe 基于 iframe 的 PaymentElement)中提交表单。
let form = document.getElementById('theForm');
let div = document.getElementById('placeOrder');
let span = document.createElement('span');
form.addEventListener('submit', function(e){
e.preventDefault();
div.style.display = 'none';
span.innerText = 'processing...';
form.appendChild(span);
console.log('form is submitted.');
})
这就是你应该如何处理这个问题 这个答案是针对面临此类问题的新开发人员的。
评论
<form>
document.theForm.submit();
不起作用,您宁愿在 HTML 中使用和指定表单 ID,例如:document.getElementById("theForm").submit();
<form id="theForm">(content)</form>