为什么ajax调用后页面刷新 [英] Why page is refreshing after ajax call

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

问题描述

我有一个活动.如果是模糊的,则页面没有刷新,但是如果是单击事件,则页面正在重新加载.原因是什么?

I have one event. If it is blur then page is not refreshing but if it is click event then page is reloading. What is the reason for the same.

$('#weeklygeneratebtn').on('click',function(){
    $('#store_name').on('blur',function(){
        var store_id = $("input[name=store_id]").val();
        alert($(this).attr("data-indent"));
        var category = '';
        var flag;
        if($(this).data("indent") == "sundry"){
            category = '235';
            flag = 1;
        }
        if(flag== 1){
            $.ajax({ url: 'lib/function.php',
                data: {action: 'getStoreSupplyItems', 
                       id:store_id, 
                       indent:1, 
                       category:category},
                success: function(output) {
                     //alert(output);
                     $('#response').html(output);
                }     
            });
        }

这是我的代码.如果在ajax调用后选择click函数,则页面将刷新.但是,如果我选择模糊功能,则不会重新加载页面.我的第一个疑问是重新加载的原因是什么,第二个问题是在点击情况下不重新加载该怎么办.

This is my code.If select the click function after ajax call the page is refreshing. But if I select the blur function the page is not reloading. My first doubt is what is the reason for reloading and my second question is what should I do for not reloading in click case.

<button id="weeklyindentbtn" style="width:120px" class="button ui-button ui-widget ui-state-default ui-corner-all action-button ui-button-text-only" data-role="button">&nbsp;&nbsp;Generate</button>

推荐答案

如果您的按钮是类型为"submit"的"input"元素,则在Javascript单击处理程序中,您需要在函数调用,然后包含preventdefault函数,如下所示:

If your button is an "input" element with a type of "submit" then in the Javascript click handler you need to pass the "event" parameter in the function call and then include the preventdefault function, like this:

$("#weeklygeneratebtn").click(function(event) { event.preventDefault(); ... });

这将阻止正常行为(即回发)的发生.

This will stop the normal behaviour (i.e. a postback) from occurring.

如果您的按钮是按钮"元素,则可以向其添加按钮"类型以防止回发,如下所示:

If your button is a "button" element then you can add the "button" type to it to prevent postbacks, like this:

<button type="button" id="weeklygeneratebutton">Generate</button>

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

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