Reputation: 2907
I got a form which I would like to submit using jQuery if the result of checkBrowser
function is old
.
<form action="xxx" method="post">
...
<input id="iop" type="submit">
...
</form>
jQuery:
$("#iop").mousedown(function(e){
e.preventDefault();
var status=checkBrowser();
if(status=='old'){
$("#iop").submit();
}
});
What I have done:
I debugged the code and it reaches the line $("#iop").submit();
but nothing happens. any idea?
Upvotes: 2
Views: 81
Reputation: 38102
You need to use submit()
on <form>
not <input>
element. From the docs:
The submit event is sent to an element when the user is attempting to submit a form. It can only be attached to elements
if(status=='old'){
$("form").submit();
}
or more specific by using .closest() to get the closest matching form
element of your submit button when traverse up the DOM tree:
if(status=='old'){
$(this).closest('form').submit();
}
Upvotes: 4
Reputation: 2401
Change <input id="iop" type="submit">
to <input id="iop" type="button">
and $("#iop").submit();
to $("form").submit();
Try this
<form action="xxx" method="post">
<input id="iop" type="button" value="Submit">
</form>
<script src="http://code.jquery.com/jquery-latest.min.js"
type="text/javascript"></script>
<script>
$(document).ready(function(){
$("#iop").mousedown(function(e){
//e.preventDefault();
// var status=checkBrowser();
status = "old";
if(status=='old'){
$("form").submit();
}
});
});
</script>
Upvotes: 1
Reputation: 13003
Now you are refering to the submit input
instead of the form
.
Change this:
$("#iop").submit();
To this:
$("form").submit();
Upvotes: 2
Reputation: 780818
Instead of calling submit()
explicitly, let the default action do it:
$("form").submit(function(e){
var status=checkBrowser();
if(status != 'old'){
e.preventDefault();
}
});
Also, bind the handler to the form's submit
event, not the button.
Upvotes: 1