$(“#").submit与Ajax刷新页面一起提交 [英] $("#").submit with Ajax refreshing page

查看:152
本文介绍了$(“#").submit与Ajax刷新页面一起提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不知道是什么问题,因为这段代码可以在一段时间前正常工作,我正在尝试使用Ajax提交表单,这是我的代码

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

 $(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;
   });
});

HTML代码

<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>

我的问题是,当我单击提交"按钮时,页面正在刷新,并且根本没有提交Ajax调用. 不知道发生了什么.

My issue is, when I am clicking on submit button, page is getting refresh and no Ajax call is being submitted at all. Not sure what is happening.

推荐答案

我认为您需要在事件对象上调用preventDefault.

I think you need to call preventDefault on event object.

当您使用普通JS处理程序时,返回false是可以的.

Returning false is okay when you're in the vanilla JS handler, and you're not.

这篇关于$(“#").submit与Ajax刷新页面一起提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆