2011-11-16 53 views
1

全部,使用jquery在AJAX響應中提取表單變量

我有一個調用URL的Jquery ajax請求。我收到的ajax響應是一個帶有一個隱藏變量的HTML表單。只要我的ajax請求成功,我想檢索隱藏變量的值。我怎麼做?

Example: 

html_response for the AJAX call is : 
<html><head></head><body><form name="frmValues"><input type="hidden" 
name="priceValue" value="100"></form></body></html> 


$.ajax({ 
     type: 'GET', 
     url: "/abc/xyz/getName?id="+101, 
     cache: false, 
     dataType: "html", 
     success: function(html_response) 
     { 
      //Extract form variable "priceValue" from html_response 
      //Alert the variable data.         
     } 
     }); 

感謝

回答

1

,你得到的將是一個字符串html_response。因此,如果您碰巧知道頁面的外觀,您可以使用indexOf搜索文本。

...但是,這種解決方案是混亂和容易出錯。或者,您可以創建一個新的HTML元素(如div),將響應html放在那裏,然後獲取隱藏變量,就像訪問任何普通的html元素一樣。

例如:

var tempDiv = $("<div/>"); 
tempDiv.append(html_response); 
var myValue = tempDiv.find("input[name='priceValue']").val(); 
0

您可以創建jQuery對象:

var form = $(html_response); 

然後使用jQuery選擇&穿越讓你輸入PriceValue。

0

您可以使用$(html_response).find("input[name='priceValue']").val();