繁体   English   中英

$(“#”)。submit与Ajax刷新页面一起提交

[英]$(“#”).submit with Ajax refreshing page

由于此代码之前工作得很好,所以不确定是什么问题,我正在尝试使用Ajax提交表单,这是我的代码

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

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调用。 不知道发生了什么。

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

当您处于普通JS处理程序中时,返回false是可以的,而实际上并非如此。

尝试将事件作为参数传递,然后调用e.preventDefault()

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

            }
        });
    });
});

采用

event.preventDefault();


$("#login").submit(function(event) {
    event.preventDefault();
    $('#signinPane').showLoading();

这将防止默认表单提交特征。

尝试:

$("#login").submit(function(e) {
 e.preventDefault();
 e.stopPropagation();

http://api.jquery.com/event.preventDefault/

http://api.jquery.com/event.stopPropagation/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM