jQuery Load函数没有在div中加载但加载新页面

时间:2016-03-03 06:15:19

标签: php jquery html

我有以下html标记:

<button class="btn btn-success" id="search"><span class="glyphicon glyphicon-search"></span> Search</button>
<div class="row text-center" style="margin-top: 50px;">
        <div class="col-md-12">
            <div id="content-area"></div>
        </div>
    </div>

我想在#content-area中加载page.php。这是我用过的jQuery:

$("#search").click(function(){
    $("#content-area").load("page.php");
});

问题是page.php没有在#content-area内加载,但是它作为新页面本身加载。我想要在#content-area内加载page.php。请帮忙。

2 个答案:

答案 0 :(得分:1)

//你必须进行ajax调用才能在没有页面加载的情况下获取内容。

$("#search").click(function(){
          $.ajax({
                url: 'page.php', // point to server-side PHP script
                dataType: 'json', // what to expect back from the PHP script, if anything
                type: 'POST',           
            success: function (data) {
                if (data.status === 'success')
                {
                   $("#content-area").html(data);

                }
                else if (data.status === 'error')
                {
                    alert(data.message);
                }
            },
            error: function (e) {
                return false;
                Msg.show('Something went wrong, Please try again!','danger', 7000);
            }
        });    
});

答案 1 :(得分:0)

我试过下面的代码,它对我来说很好。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>

<button class="btn btn-success" id="search"><span class="glyphicon glyphicon-search"></span> Search</button>
<div class="row text-center" style="margin-top: 50px;">
        <div class="col-md-12">
            <div id="content-area"></div>
        </div>
    </div>
<script>
    $("#search").click(function(){
    $("#content-area").load("test1.php");
});
</script>