$ .ajax仅在置于alert()时有效 [英] $.ajax works only when put alert()

查看:110
本文介绍了$ .ajax仅在置于alert()时有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在以前使用AJAX加载的内容中加载内容。我把代码

I tried to load content inside content those are loaded previously with AJAX. I put the code

加载():

jQuery("#contentpage").on("submit", "#loginform", my = function() {
    $("#contentpage").load("/logincheck.jsp");
    alert("this is required ");
});

与$ .ajax():

jQuery("#contentpage").on("submit", "#loginform", function() {

    $.ajax({
        url: '/logincheck.jsp',
        success: function(data) {
            $('#contentpage').html(data);
        }
    });

    alert("this is required ");
});

#loginform 是一个加载的HTML表单之前的AJAX请求,我成功访问了 .on()函数。

#loginform is a HTML form loaded with previous AJAX request, I successfully access in with .on() function.

但问题是当我提交 #loginform alert('这是必需')框然后刷新整个页面。
#contentpage 内的内容也没有变化。

But the problem is when I submit #loginform without putting alert('this is required') box then its refreshing the whole page. Also content inside #contentpage are also not changing.

但是,如果我按照警告(这是必需的)执行相同操作,其工作正常,内容正在加载 #contentpage 而不刷新整个页面。

But if I do the same with putting alert("this is required"), its working fine, contents are getting loaded in #contentpage without refreshing whole page.

这是一个时间问题吗? Ajax请求需要时间,而且它是异步的?如果我错了,请纠正我。

Is this a timing issue? Ajax request takes time and it's asynchronous? Please correct me if I am wrong.

推荐答案

你试过这个吗?

        jQuery("#contentpage").on("submit","#loginform",my=function(event){ 
             event.preventDefault();
             $("#contentpage").load("/logincheck.jsp");
             alert("this is required ");
        });

这至少应该阻止页面刷新,也可以解决其他问题。祝你好运!

This should at least stop the page from refreshing and may sort out the other problems as well. Good luck!

这篇关于$ .ajax仅在置于alert()时有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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