[英]Javascript: how to cancel form submission without return false?
I have a form submission code, which does an AJAX call & redirects from there- if match found. 我有一个表单提交代码,如果找到匹配项,它将执行AJAX调用并从那里重定向。
But, somehow, return false is omitted & the form is getting submitted. 但是,以某种方式,返回false会被省略,并且表单将被提交。
I dont want to use return false, & needs to cancel submission for all cases. 我不想使用return false,并且需要在所有情况下取消提交。 Is there any jquery method for that? 是否有任何jquery方法?
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 HTML
<form action="" method="get" onSubmit="getLogin();">
Actual code is in Jquery-mobile & having issue with return false;
实际代码在Jquery-mobile中, return false;
出现问题return false;
in the end of the getLogin() function- it cant stop Submitting the form. 在getLogin()函数的最后-它无法停止提交表单。
Assuming that the getLogin is bound to a form submit button/link, you could use preventDefault and stopPropagation 假设getLogin绑定到表单提交按钮/链接,则可以使用preventDefault和stopPropagation
function getLogin(e)
{
//cancel form submission code here
e.preventDefault();
e.stopPropagation();
}
Get rid of onSubmit="getLogin();"
摆脱onSubmit="getLogin();"
<form id="uniqueID" action="" method="get">
$(function(){
$("#uniqueID").click(getLogin);
})
I am not sure how the getLogin()
is called? 我不确定如何调用getLogin()
? And Do you have a button like <input type="submit" onclick="getLogin()">
which submits the form. 并且您是否有一个像<input type="submit" onclick="getLogin()">
这样的按钮来提交表单。
If so, add a click handler and implement the code there as below, 如果是这样,请添加一个点击处理程序并在其中实现以下代码,
HTML: HTML:
<input type="submit" onclick="getLogin()" id="submit_btn">
JS: JS:
$('#submit_btn').click (function (e) {
e.preventDefault(); //this should stop the form submission.
getLogin();
});
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: 在阅读文档 10秒钟后,我注意到,这可以与其他地方提到的preventDefault
结合使用:
$('#myform').submit(ev, function(){
ev.preventDefault();
...
but return false
should work as well. 但return false
应该也可以。
Why dont you just set the action-Parameter right? 您为什么不正确设置action-Parameter?
action="javascript:;"
BTW: an even better solution would be to set the action-Parameter to this at $(window).load(..). 顺便说一句:更好的解决方案是在$(window).load(..)处将action-Parameter设置为此。 Why? 为什么? Because if you want the form to be usable when JS is deactivated it is necessary to do so. 因为如果您希望表单在停用JS时可用,则必须这样做。
If handler is not returning false, either a logic problem..or more likely an error thrown within handler before code reaches the return 如果处理程序未返回false,则可能是逻辑问题。或更可能是在代码到达返回值之前在处理程序中引发了错误。
Try using event.preventDefault() before your other code in handler 尝试在处理程序中的其他代码之前使用event.preventDefault()
http://api.jquery.com/event.preventDefault/ http://api.jquery.com/event.preventDefault/
function getLogin()
{
var e = window.event;
e.preventDefault();
$username = ...
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.