javascript 使用类型按钮javascript提交表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18572728/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
submit of form using type button javascript
提问by Gaurav Sood
i have a form with an element of type button. I want to use the onclick method to set an image of an img tag, and then simulate the "action" of the form. I tried the following code:
我有一个带有按钮类型元素的表单。我想使用onclick方法设置一个img标签的图像,然后模拟表单的“动作”。我尝试了以下代码:
<html>
<head>
<script type="text/javascript">
function imgtest(){
document.getElementById
("test").src="progress.gif";
}
</script>
<title>Hello</title>
<body>
<form method="POST" action="test.php">
<input type="button" value="Submit"
onclick="imgtest()">
</form>
<div id="img">
<img id="test" src="load.png">
</div>
</body>
</html>
Though, this does not seem to work. What may be the other solution?
虽然,这似乎不起作用。其他解决方案可能是什么?
采纳答案by marekful
While type="submit"
controls do submit their form automatically, type="button"
s do not. You can trigger the submission with JavaScript by executing
虽然type="submit"
控件会自动提交表单,但type="button"
s 不会。你可以通过执行 JavaScript 来触发提交
this.form.submit();
at the end of the click event handler of the button (that is inside the form).
在按钮的单击事件处理程序的末尾(即在表单内)。
There is no need to use jQuery or to give an ID to the form, as form controls always refer back to their form.
无需使用 jQuery 或为表单提供 ID,因为表单控件总是引用回它们的表单。
回答by Vinay Pratap Singh
you can do it like this
你可以这样做
give an id to your form
给你的表单一个 id
<form method="POST" action="test.php" id="someid">
on button click method in jquery
jquery中的按钮点击方法
$('#buttonid').click(
function(){
$("#someid").submit();
});
use document.getElementById('formidhere').submit();
//for javascript solution
使用document.getElementById('formidhere').submit();
//for javascript 解决方案
回答by Butani Vijay
First give the id to your form
首先给你的表单提供 id
ex.
前任。
<form method="POST" action="test.php" id="testForm">
then in you button click event write as below in your imgtest()function :
然后在你的按钮点击事件中在你的imgtest()函数中写如下:
document.getElementById('testForm').submit();
回答by Indra Yadav
function imgtest(){
document.getElementById("test").src="progress.gif";
var frm=document.getElementById("frm1");
frm.submit();
}
回答by Yasitha
In Javascript
在 JavaScript 中
document.getElementById('form').submit();
In JQuery
在 JQuery 中
$('#button').click(function() {
$('#form').submit();
});