嗨,我发了这两个标签
<input type="text" name="aav" class="aav"/>
<div id="aridc" class="aridc"></div>在此上下文中
$(".aav").live("keyup",function(){
$.ajax({
type: "POST",
url: "recapVente.html",
cache: false,
data: "exp="+$(this).val()+" article",
success: function(reponse){
$(this+"~.aridc").empty();
$(this+"~.aridc").html(reponse);
},
error: function(e){
$(this+"~.aridc").empty();
$(this+"~.aridc").html("Votre recherche contien des erreurs");
}
});
});为了选择div并确保它是输入后的div,我这样做
$(".aav~.aridc")但是当我这样做的时候
$(this+"~.aridc")我没有回应
请使用以下方式选择div
this而不是
.aav这里
$(".aav~.aridc")谢谢
发布于 2012-05-04 23:56:29
这是因为ajax方法的回调中的this没有引用相同的对象;上下文发生了变化。您可以通过保留对调用者的引用来轻松解决此问题:
$(".aav").live("keyup",function(){
var that = this;
$.ajax({
type: "POST",
url: "recapVente.html",
cache: false,
data: "exp="+$(this).val()+" article",
success: function(reponse){
$(that.class+"~.aridc").empty();
$(that.class+"~.aridc").html(reponse);
},
error: function(e){
$(that.class+"~.aridc").empty();
$(that.class+"~.aridc").html("Votre recherche contien des erreurs");
}
});});
发布于 2012-05-04 23:57:32
将此引用存储到一个变量,并使用.next()获取同级:
var thediv = this;
...
$(thediv).next(".aridc")发布于 2012-05-05 00:00:48
this是一个DOM元素,而不是字符串,不能强制将其放入字符串的选择器中。您只需再次输入字符串(或者使用.attr来获取类的某些特定属性选择器,比如它的ID)。不仅如此,而且您使用的this也是错误的。有了.ajax,我相信它将是jqXHR。您可以使用context:或将this存储到另一个变量。
我发现有趣的是,~使用选择器上下文。您可以使用:
$input = $(".aac");
$("~ .aridc", $input)它的工作原理与
$(".aac ~ .aridc");https://stackoverflow.com/questions/10452148
复制相似问题