[英]$(“#”).submit with Ajax refreshing page
Not sure what is the issue as this code was working fine some time earlier, I am trying to submit my form using Ajax
and this is my code 由于此代码之前工作得很好,所以不确定是什么问题,我正在尝试使用
Ajax
提交表单,这是我的代码
$(function() {
$("#login").submit(function() {
$('#signinPane').showLoading();
$("#loginError").hide();
var data = $(this).serializeObject();
$.ajax({
'type': "POST",
'url': "<c:url value="/shop/customer/j_spring_security_check"/>",
'data':data,
'success': function(result) {
}
});
return false;
});
});
<form id="login" method="post" accept-charset="UTF-8">
<input id="userName" style="margin-bottom: 15px;" type="text" name="j_username" size="30" />
<input id="password" style="margin-bottom: 15px;" type="password" name="j_password" size="30" />
<button type="submit" id="login-button"></button>
</form>
My issue is, when I am clicking on submit button, page is getting refresh and no Ajax call is being submitted at all. 我的问题是,当我单击“提交”按钮时,页面正在刷新,并且根本没有提交任何Ajax调用。 Not sure what is happening.
不知道发生了什么。
I think you need to call preventDefault
on event object. 我认为您需要在事件对象上调用
preventDefault
。
Returning false is okay when you're in the vanilla JS handler, and you're not. 当您处于普通JS处理程序中时,返回false是可以的,而实际上并非如此。
Try passing the event as a parameter and then calling e.preventDefault()
尝试将事件作为参数传递,然后调用
e.preventDefault()
jsfiddle Documentation jsfiddle 文档
$(function() {
$("#login").submit(function(e) {
e.preventDefault();
$('#signinPane').showLoading();
$("#loginError").hide();
var data = $(this).serializeObject();
$.ajax({
'type': "POST",
'url': "<c:url value="/shop/customer/j_spring_security_check"/>",
'data':data,
'success': function(result) {
}
});
});
});
use 采用
event.preventDefault();
$("#login").submit(function(event) {
event.preventDefault();
$('#signinPane').showLoading();
This will prevent default form submit characteristic. 这将防止默认表单提交特征。
Try: 尝试:
$("#login").submit(function(e) {
e.preventDefault();
e.stopPropagation();
http://api.jquery.com/event.preventDefault/ http://api.jquery.com/event.preventDefault/
http://api.jquery.com/event.stopPropagation/ http://api.jquery.com/event.stopPropagation/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.