jQuery成功:function {}问题

时间:2010-05-02 15:39:47

标签: jquery ajax select

我在页面上有几个'select'元素。当我选择一些选项时,ajax请求将被发送到服务器,并且必须使用响应值更新与此“select”相邻的元素。我希望以下代码能够正常运行:

$(".vars").live("change", function() { //selected something in <select> list
    $.ajax({
        type: "POST",
        url "someurl.php",
        data: {somedata},
        success: function(html) {
            $this.next().html(html); //this does not update .next() element.
        }
    });
});

如果我更换

$(this).next().html(html);

alert(html);

我可以看到ajax请求成功了。此外,仅当页面上只有一个“选择”时才有效,否则会显示空白弹出窗口。

2 个答案:

答案 0 :(得分:1)

$(".vars").live("change", function() { //selected something in <select> list
    var $this = $(this);
    $.ajax({
        type: "POST",
        url "someurl.php",
        data: {somedata},
        success: function(html) {
            $this.next().html(html); //this does not update .next() element.
        }
    });
});

答案 1 :(得分:1)

我相信在你的回调中引用时,“this”并不是你认为的那样。试试这个:

$(".vars").live("change", function() { //selected something in <select> list 
    var $driver = $(this);
    $.ajax({ 
        type: "POST", 
        url "someurl.php", 
        data: {somedata}, 
        success: function(html) { 
            $driver.next().html(html); //this does not update .next() element. 
        } 
    }); 
});