0

嗨,我发了这两个标签

<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 并确保它是输入之后的那个我这样做

$(".aav~.aridc")

但是当我这样做时

$(this+"~.aridc")

我没话可说

请如何使用

this

代替

.aav

这里

$(".aav~.aridc")

谢谢

4

3 回答 3

0

这是因为this在 ajax 方法的回调内部没有引用同一个对象;上下文发生了变化。您可以通过保留对调用者的引用来轻松解决此问题:

 $(".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-04T15:56:28.750 回答
0

将此引用存储到一个变量,并用于.next()获取兄弟:

var thediv = this;
...
$(thediv).next(".aridc")
于 2012-05-04T15:57:32.213 回答
0

this是一个 DOM 元素,而不是字符串,它不能被强制转换为字符串的选择器。您只需再次键入字符串(或者我们.attr为类获取一些特定的属性选择器,例如它的 ID)。不仅如此,而且this无论如何你都在使用错误的方法......我相信.ajax它会是。jqXHR您可以使用context:或存储this到另一个变量。

我发现有趣的是它~适用于选择器上下文。你可以使用:

$input = $(".aac");
$("~ .aridc", $input)

它的工作方式与

$(".aac ~ .aridc");
于 2012-05-04T16:00:48.270 回答