2012-08-06 33 views
0

jquery的$ .POST:

$.post(
    'includes/studiesAjax/addTryb.php', 
    {inputVL: inputVL}, 
    function(res){ 
     $("#container").html(res); 
    } 
); 

響應是長HTML代碼

我想從響應beetween <p>標籤的第一行中提取數據時,它從反應中分離,並分配給新的變量。怎麼做?如果返回JSON而不是HTML,你可以像這樣

$.post(
      'includes/studiesAjax/addTryb.php', 
      {inputVL: inputVL}, 
      function(res){ 
       var result = $(res).find('p:first').html(); 
       $("#container").html(result); 
      } 
     ); 
+0

我不關注;你能澄清你的意圖嗎? – Utkanos 2012-08-06 10:12:15

+2

不是更好的返回一個更結構化的repsonse(比如JSON,或者至少只有你需要的那部分),所以你不需要後處理? – Nanne 2012-08-06 10:13:07

回答

3
function(res){ 
    var newVar = $(res).find('p:first').html(); 
} 
0

你應該轉變爲jQuery對象的反應,然後你可以像往常一樣採取行動。

 $.post(
      'includes/studiesAjax/addTryb.php', 
      {inputVL: inputVL}, 
      function(res){ 
       var data = JSON.parse(res); 
       if(data && data.response1){ 
        result = (data.response1); 
        console.log(result); 
       } 
       if(data && data.response2) 
        $("#container").html(data.response2); 
      } 
     ); 
0