This is what I got:
<form method="post" id="myform" class="myform">
<input type="submit" onclick="return claim();" name="submit" class="onsubmit" value="" />
</form>
function claim() {
var c = confirm('You sure?');
if (!c) {
return false;
}
var password = prompt("Please mention pw","");
if (password != null && password != "") {
$.post(
"/claim/",
{ partner_pwd: password },
function(data) {
if (data == '1') {
$('form').submit(function() {
alert('Handler for .submit() called.');
});
}
});
}
return false;
}
This works well, but my problem is that it won't submit the form. I tried both $('form').submit()
and $('#myform').submit()
inside the data == '1'
statement. I tried alert(1)
inside this if statement, and it displayed fine, I only need to make it submit the form, why wont this work?
Update:
Console says:
Uncaught TypeError: Property 'submit' of object #<HTMLFormElement> is not a function jquery.min.js:3 f.event.trigger
You made the following mistakes:
'form'
is too general and returns an array. So change it to '#myform'
.return false;
. Even after successfully validating the form, this line blocked it. It should have been return true;
. This mistake was pointed out by @11684, but no one understood him and was downvoted by some.Anyways, here's the debugged code.
function claim()
{
var c = confirm('You sure?');
if (!c) {
return false;
}
var password=prompt("Please mention pw","");
if (password!=null && password!="")
{
$.post("/claim/", { partner_pwd: password },
function(data) {
if(data == '1')
{
$('#myform').bind('submit',function() {
alert('Handler for .submit() called.');
});
}
});
}
return true;
}
To raise an event, you call the relevent function without a parameter like this:
$('form').submit();
Your current code is simply adding a submit handler function to be executed when the form is submit, it is not actually submitting the form.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With