使用类型按钮javascript提交表单

submit of form using type button javascript

本文关键字:提交 表单 javascript 按钮 类型      更新时间:2023-09-26

我有一个带有button类型元素的表单。我想使用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>

不过,这似乎并不奏效。另一种解决方案是什么?

你可以像这样做

给你的表单一个id

<form method="POST" action="test.php" id="someid">

jquery 中的按钮点击方法

$('#buttonid').click(
function(){
 $("#someid").submit();
  });

document.getElementById('formidhere').submit();//用于javascript解决方案

虽然type="submit"控件会自动提交表单,但type="button"控件不会。您可以通过执行来触发JavaScript提交

this.form.submit();

在按钮的点击事件处理程序的末尾(在表单中)。

不需要使用jQuery或为表单提供ID,因为表单控件总是引用回它们的表单。

在Javascript 中

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

在JQuery 中

$('#button').click(function() {
    $('#form').submit();
});

首先为表单提供id

前任。

<form method="POST" action="test.php" id="testForm">

然后在按钮中单击事件,在imgtest()函数中按如下方式写入:

document.getElementById('testForm').submit();
function imgtest(){
document.getElementById("test").src="progress.gif";
var frm=document.getElementById("frm1");
frm.submit();

}