繁体   English   中英

jQuery ajax post href值,无需刷新页面

[英]jquery ajax post href value without refreshing the page

您好,我想发布链接值,例如href =“?id = 1”我的链接

但是没有刷新当前页面,它刷新了页面,但是我不想重新加载页面,这是我的代码,请帮助我

function loadTabContent(tabUrl){
    $("#preloader").show();
    jQuery.ajax({
        type: post,
        url: tabUrl, 
        data: "id="+country_id,
        cache: false,
        success: function(message) {
        jQuery("#tabcontent").empty().append(message);
        $("#preloader").hide();
        }
    });
}

jQuery(document).ready(function(){  

    $("#preloader").hide(); 
    jQuery("[id^=tab]").click(function(){   

        // get tab id and tab url
        tabId = $(this).attr("id"); 
        tabUrl = jQuery("#"+tabId).attr("href");

        jQuery("[id^=tab]").removeClass("current");
        jQuery("#"+tabId).addClass("current");

        // load tab content
        loadTabContent(tabUrl);
        return false;
    });
});

尝试这个 :

function loadTabContent(tabUrl){
    $("#preloader").show();
    jQuery.ajax({
        type: post,
        url: tabUrl, 
        data: "id="+country_id,
        cache: false,
        success: function(message) {
        jQuery("#tabcontent").empty().append(message);
        $("#preloader").hide();
        }
    });
}
jQuery(document).ready(function(){  

    $("#preloader").hide(); 
    jQuery("[id^=tab]").click(function(e){   

        // get tab id and tab url
        tabId = $(this).attr("id"); 
        tabUrl = jQuery("#"+tabId).attr("href");

        jQuery("[id^=tab]").removeClass("current");
        jQuery("#"+tabId).addClass("current");

        // load tab content
        loadTabContent(tabUrl);
        e.preventDefault();
    });
});

我认为return false应该没问题,除非您的函数导致发生异常,例如错误的ajax请求。 进行ajax调用时,请尝试在type属性中的单词post周围加上引号。 另外country_id是未定义的,如果需要也应该传递它。

http://api.jquery.com/jquery.ajax/

function loadTabContent(tabUrl, country_id){
    $("#preloader").show();
    jQuery.ajax({
        type: 'POST',
        url: tabUrl, 
        data: "id="+country_id,
        cache: false,
        success: function(message) {
            jQuery("#tabcontent").empty().append(message);
            $("#preloader").hide();
        }
    });
}

暂无
暂无

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

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