如何在ajax调用后停止刷新页面? [英] How to stop refreshing page after ajax call?

查看:619
本文介绍了如何在ajax调用后停止刷新页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在ajax调用后停止刷新页面。我试过把e.preventDefault();并返回false;同样,但我的页面再次令人耳目一新。

I am unable to stop refreshing page after ajax call. I have tried by putting e.preventDefault(); and return false; as well but again my page is refreshing.

我不知道代码有什么问题。请在ajax调用后帮我停止刷新页面。解决这个问题对我来说将是一个很大的帮助。提前致谢。

I dont know what is the problem with the code or something. Please help me to stop refreshing page after ajax call. solving this issue would be a great help for me. Thanks in advance.

这是我的代码:

$(document).ready(function() {
    $('#loginForm').on('click', function(e) {
        e.preventDefault();
        var formData = {
            'uname'  : $('#uname').val(),
            'pwd'    :      $('#pwd').val()
        };
        $.ajax({
            type        : "POST",
            url         : "getresults.php", 
            data        : formData
        }).done(function(data) {
              alert(data+"This is working");
        }).fail(function(data) {
              alert("This is not working");
        });
    });
});


推荐答案

id #loginForm 指向表单?如果是,则需要收听提交事件而不是单击。如果你真的需要监听click事件,你必须将事件绑定到提交按钮或触发 form.submit()

Is the id #loginForm pointing to a form? If yes you need to listen to the submit event instead of click. If you really need to listen to the click event, you have to bind the event to the submit button or whatever triggers the form.submit().

尝试这样的事情:

$('#loginForm').on('submit', function(e) {
    e.preventDefault();
    e.stopPropagation(); // only neccessary if something above is listening to the (default-)event too

    [YOUR CODE GOES HERE]
});

这应该可以解决这个问题。

This should do the trick for you.

这篇关于如何在ajax调用后停止刷新页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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