[問題] 使用ajax時,回應的html只能append一次??
各位好,我的情形是這樣的:
有2個檔案分別是index.html和a.php,我想做的事情是在index.html利用ajax對
a.php請求,而a.php會動態產生多個select下拉式選單,然後再將產生好的選單
傳回(append)index.html內。
也就是說,使用者在index.html內選擇不同的option,都會向a.php作請求,然後
將對應的所有select選單傳回index.html。
但是目前做出來的結果卻只有第一次選擇option時,a.php的內容可以append進
index.html,之後的選擇都沒反應......。不斷的測試後發現問題出在append的
地方,程式碼如下:
<script language="javascript">
$(function(){
var html = $.ajax({
type:"GET",
url:"a.php",
data:"給a.php的參數",
async: false
}).responseText;
$("#content_main").append(html);
});
function click_f(){
$("#sw_loc").change(function(){
var sw_con = $('#sw_loc :selected').val();
var html2 = $.ajax({
type:"GET",
url:"a.php",
data: sw_con ,
async: false
}).responseText;
//alert(html2);
//以上印出html2內容
$("#content_main").empty();
$("#content_main").append(html2);
});
}
</script>
<body onLoad="click_f();">
<div id="content_main"></div>
</body>
html2為a.php回傳的資料(即下拉式選單的html),#sw_loc為a.php所產生的下拉式
選單。
若把紅色的地方刪掉,則能確定每次對a.php的要求的內容(即html2)都正確無誤。
想請教各位是否是我對append的使用錯誤,還是其他的問題導致只有第一次選擇
option時執行正確??
※ 編輯: pillowYUI 來自: 61.216.153.2 (01/26 00:50)
※ 編輯: pillowYUI 來自: 61.216.153.2 (01/26 00:52)
※ 編輯: pillowYUI 來自: 61.216.153.2 (01/26 00:52)
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 1 之 2 篇):
Ajax 近期熱門文章
PTT數位生活區 即時熱門文章