2014-08-29 21 views
2

在主頁上,有一個<div>與ID - "search_result"顯示所有的搜索結果。搜索結果是通過ajax調用加載的。結果(子頁面)直接放入div負載阿賈克斯導致主(父)頁面的DIV

在子頁面,我能趕上click事件,做一套ajax呼叫接受的結果。我想將結果加載回主頁的div"search_result"。我不能。

這裏是主要頁面的部分HTML。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script> 
    <title>search</title> 
    ...... 
    </head> 
    <body> 
    ...... 
    <div id="search_results"></div> 
    </body> 
</html> 

這是裝入"search_results"div子頁面的內容。

<script> 
$(document).ready(function() { 

    $("#update_op").click(function(event){ 
     event.preventDefault(); 
     var link = $(this).attr("href"); 
     // Ajax here 
     $.ajax({ 
      type: "POST", 
      url: link, 
      dataType: "json", 
      success:function(data) { 
       $("#search_results").html(data); 
      } 
     }); 
     return false; // for good measure 
    }); 
}); 
</script> 


<div class="div_table"> 
    <div id="item_row_4" class="div_row"> 
    <div class="div_cell"> 
     <a id="update_op" class="update_op op" href="http://abc/bk/v/show-update/id/4">update</a>&nbsp; 
    </div> 
    </div> 
</div> 

$("#search_results").html(data);此行未加載我收到的結果。

解決方法:將dataType更改爲html並且它有效。

+3

也許你想改變'的dataType :「html」'或'dataType:「text」'?將一個JSON對象追加到一個div沒有多大意義。 – dave 2014-08-29 22:19:54

+0

看起來像是我的錯。我將其更改爲html。似乎現在工作。 – 2014-08-29 22:43:46

回答

1

我有這個問題,這裏是我的解決方案:

<script> 
$(document).ready(function() { 

    $("#update_op").click(function(event){ 
     event.preventDefault(); 
     var link = $(this).attr("href"); 
     // Ajax here 
     $.ajax({ 
       type: "POST", 
       url: link, 
       dataType: "json", 
       success:function(data) { 
          location.reload(); 
         } 
     }); 
     return false; //for good measure 
    }); 
}); 
</script> 

我希望這可以幫助。

0

你爲什麼要使用「數據類型」 JSON,你不發送JSON,所以我不知道,你不應該把這些代碼。

這裏是一個文件名爲「probando.php」的例子。它適合我。

<body> 
<div id="search_results"></div> 
<br /><br /> 
<div class="div_table"> 
    <div id="item_row_4" class="div_row"> 
     <div class="div_cell"> 
      <a id="update_op" class="update_op op" href="probando.php">update</a>&nbsp; 
     </div> 
    </div> 
    </div> 
<script type="text/javascript"> 
$(document).ready(function() { 

    $("#update_op").click(function(event){ 
     event.preventDefault(); 
     var link = $(this).attr("href"); 
     // Ajax here 
     $.ajax({ 
      url: link, 
      type: 'POST', 
      success: function (response){ 
       $("#search_results").html(response); 
      } 
     }); 
    }); 
}); 
</script> 

和 「probando.php」 代碼:運行時加載的DOM元素

<?php 
    echo "Hola Mundo!"; 
?> 
2

點擊事件具有以這種方式被寫入

$("#update_op").on('click',function(event){ 
    e.preventDefault(); // return false 
    $.ajax({ 
     url: "url to fetch result", 
     type: 'POST' 
    }).done(function(data){ 
     $("#search_results").html(data); // this would change the data, you could use append(), prepend() etc. you would have to process data to display with css if needed 
    }); 
});