2012-05-04 108 views
0

嗨,我發前一個div論文兩個標籤選擇由輸入

<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並sured,它是一個跟隨輸入我做這

$(".aav~.aridc") 

,但是當我這樣做

$(this+"~.aridc") 

我都沒有反應

請如何選擇使用

this 

,而不是

.aav 

這裏

$(".aav~.aridc") 

感謝

股利
+0

您可以在這裏顯示HTML和jQuery代碼? – Thulasiram

+0

也許試試jQuery prev:http://api.jquery.com/prev/ – BiAiB

+0

你原來的函數範圍之外! – adeneo

回答

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"); 
     } 
    }); 

});

+0

我不認爲這會工作..'that +「」'導致jQuery內部的語法錯誤。 –

+0

謝謝,但我有問題沒有答案顯示 – chillo

+0

@tandu你是對的我有一個錯誤未捕獲錯誤:語法錯誤,無法識別的表達式:[對象HTMLInputElement] – chillo

0

店這個參考給一個變量,並使用.next()得到兄弟:

var thediv = this; 
... 
$(thediv).next(".aridc") 
0

this是一個DOM元素,而不是一個字符串,它不能被裹挾到選擇的字符串。您只需再次鍵入字符串(或者我們.attr以獲得該類的某些特定屬性選擇器,例如其ID)。不僅如此,但你正在與錯誤this無論如何..與.ajax它將是jqXHR我相信。您可以使用context:或將this存儲到另一個變量。

我覺得有趣的是,~與選擇器上下文一起工作。你可以使用:

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

,它會工作一樣

$(".aac ~ .aridc"); 
+0

謝謝,我想用這個! – chillo