且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用ajax和jquery替换div内容

更新时间:2023-12-05 08:29:22

您的代码具有定义onclick操作且不会自行进行调用的函数.我敢打赌,如果您双击该链接将起作用,但是您应该这样做:

Your code has a function which defines an onclick action and doesn't make the call itself. I bet if you double clicked the link it would work, but you should do it like this:

function MakeRequest(id)
{
    $.ajax({
        url : 'display.php',
        data:{"id":id},
        type: 'GET',

        success: function(data){
            $('#ResponseDiv').html(data);
        }
    });
}

最后,将呼叫更改为此:

Finally, change the call to this:

onclick='MakeRequest(5);'

只需执行此操作,它将li元素绑定到click函数,而无需"onclick":

OR just do this, which binds the li element to the click function and no "onclick" is necessary:

$(document).ready(function()
{
    $("#page_num li").click(function() {
       var id=$(this).attr(id);
        $.ajax({
        url : 'display.php',
        data:{"id":id},
        success: function(data){
            $('#ResponseDiv').html(data);
        }
    });
});
});