Reputation: 8299
I have a form submission code, which does an AJAX call & redirects from there- if match found.
But, somehow, return false is omitted & the form is getting submitted.
I dont want to use return false, & needs to cancel submission for all cases. Is there any jquery method for that?
function getLogin()
{
//cancel form submission code here
$username = $('#username').val();
$password = $('#password').val();
console.log('Logging- '+$username+' - '+$password);
callAjaxService($username, $password); // redirects according to response from this method
}
HTML
<form action="" method="get" onSubmit="getLogin();">
Actual code is in Jquery-mobile & having issue with return false;
in the end of the getLogin() function- it cant stop Submitting the form.
Upvotes: 0
Views: 1977
Reputation: 8299
function getLogin()
{
var e = window.event;
e.preventDefault();
$username = ...
...
}
Upvotes: 0
Reputation: 31250
Assuming that the getLogin is bound to a form submit button/link, you could use preventDefault and stopPropagation
function getLogin(e)
{
//cancel form submission code here
e.preventDefault();
e.stopPropagation();
}
Get rid of onSubmit="getLogin();"
<form id="uniqueID" action="" method="get">
and then use ready
$(function(){
$("#uniqueID").click(getLogin);
})
Upvotes: 2
Reputation: 6825
i dont know for sure if this work, but this i would try rigging the submit handler:
$('#myform').submit(function(){
var user = $('#username').val();
var pw = $('#password').val();
$.ajax(..., { success: function(data){
if( data.condition )$('#myform').submit();
else window.location = 'redirect.html';
});
return false;
});
after reading docs for 10 secs, i noticed, this can be combined with the preventDefault
mentioned elsewhere:
$('#myform').submit(ev, function(){
ev.preventDefault();
...
but return false
should work as well.
Upvotes: 1
Reputation: 79830
I am not sure how the getLogin()
is called? And Do you have a button like <input type="submit" onclick="getLogin()">
which submits the form.
If so, add a click handler and implement the code there as below,
HTML:
<input type="submit" onclick="getLogin()" id="submit_btn">
JS:
$('#submit_btn').click (function (e) {
e.preventDefault(); //this should stop the form submission.
getLogin();
});
Upvotes: 1
Reputation: 171679
If handler is not returning false, either a logic problem..or more likely an error thrown within handler before code reaches the return
Try using event.preventDefault() before your other code in handler
http://api.jquery.com/event.preventDefault/
Upvotes: 0
Reputation: 1259
Why dont you just set the action-Parameter right?
action="javascript:;"
BTW: an even better solution would be to set the action-Parameter to this at $(window).load(..). Why? Because if you want the form to be usable when JS is deactivated it is necessary to do so.
Upvotes: 0