我在页面上有几个'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请求成功了。此外,仅当页面上只有一个“选择”时才有效,否则会显示空白弹出窗口。
答案 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.
}
});
});