作者pillowYUI (枕由衣)
看板Ajax
标题[问题] 使用ajax时,回应的html只能append一次??
时间Tue Jan 26 00:40:46 2010
各位好,我的情形是这样的:
有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)