2013-08-12 63 views
3

我在頁面中有一個ajax調用,我用於此特定任務的jQuery庫。在ajax調用的響應中,我想分析響應消息。與Internet Explorer的jQuery篩選器()兼容性6-7-8

的問題是,這個代碼給我在IE 6-7-8錯誤消息(古怪的是,IE 9完美的作品和Firefox的作​​品完美):

User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; InfoPath.2; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729) 
Timestamp: Mon, 12 Aug 2013 08:20:03 UTC 


Message: Object doesn't support this property or method 
Line: 94 
Char: 4 
Code: 0 

任何想法是高度讚賞。看起來這此行生成錯誤:

response_str = $(server_response).filter("#response").val(); 

複製我的代碼相關的部分:

$.ajax({ 
    type:'POST', 
    url:'ajax.php', 
processData: 'false', 
data:{ 
    data1: 'val1' 
    ajax:'true' 
     }, 
dataType: "html", 
contentType: ''application/x-www-form-urlencoded'' 
    }) 
    .done(function(server_response) { 
     //the following line generate error 
     response_str = $(server_response).filter("#response").val(); 
      } 
    }) 
}'); 
+3

你使用的是什麼版本的jQuery? jQuery 2不支持IE 8和更舊的版本。 –

+2

你的代碼有很多這樣的東西:'type:''POST'','。這不可能是你正在運行的實際代碼。 –

+0

@Jonathan:謝謝我檢查版本 – czupe

回答

1

爲什麼不find()呢?

response_str = $(server_response).find("#response").val(); 

response_str = $(server_response).filter(function(i, el){ 
    return $(el).is('#response'); 
}).val(); 

上述代碼假定有僅是id爲 「應答」 的一個要素。

+0

謝謝你的回答。這裏是一個關於查找和過濾器的很好的描述:http://www.mkyong.com/jquery/difference-between-filter-and-find-in-jquery/ 請檢查出來,但基本上答案是因爲我在我的回覆中也有兄弟姐妹節點,不僅是孩子... – czupe

+0

@czupe我真的不明白這個問題。您正在尋找html響應中某個特定id的元素。無論html結構如何,您都可以通過'find'和'filter'找到它。我會用另一個'filter'選項更新答案,因爲你似乎更喜歡這個選項。 – Johan

+0

我非常感謝你的工作,所以我將upvote它,但我請你檢查這個例子:http://www.mkyong.com/wp-content/uploads/jQuery/jQuery-find-filter-example.html 我可以向你保證,我第一次嘗試與發現,這是行不通的(找不到元素:() – czupe