admin管理员组

文章数量:1316399

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:

<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?

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:

<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?

Share Improve this question asked Sep 2, 2013 at 11:57 Gaurav SoodGaurav Sood 6904 gold badges17 silver badges39 bronze badges 1
  • 1 Why not change the image on the form's onSubmit event? – Rob Farr Commented Sep 2, 2013 at 12:00
Add a ment  | 

5 Answers 5

Reset to default 4

you can do it like this

give an id to your form

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

on button click method in jquery

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

use document.getElementById('formidhere').submit(); //for javascript solution

While type="submit" controls do submit their form automatically, type="button"s do not. You can trigger the submission with JavaScript by executing

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.

In Javascript

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

In JQuery

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

First give the id to your form

ex.

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

then in you button click event write as below in your imgtest() function :

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


}

本文标签: htmlsubmit of form using type button javascriptStack Overflow