【发布时间】:2016-04-29 08:44:11
【问题描述】:
我的 AJAX 工作正常,但是当我想填充选择框时,我什么也没显示:
我的 HTML:
<div id="sim-div"></div>
我的 JS:
$(document).on('change', '#hotspotList', function(){
var selectedHotspots = $('#hotspotList').val();
$.ajax({
url: "simList.php",
type: "POST",
dataType:"json", //expect json value from server
data: selectedHotspots
}).done(function(data){ //on Ajax success
$("#sim-div").html(data.items);
})
e.preventDefault();
});
我的 PHP:
$test = '
<select id="hotspotList" class="selectpicker" data-actions-box="true" data-live-search="true" multiple>
<option>test</option>
</select>
';
echo json_encode(array('items'=>$test));
当我更改 $test= 'something'; 时,它会起作用,并且会显示“某物”这个词。
当我登录时,console.log(data.items); 我得到:
<select id="hotspotList" class="selectpicker" data-actions-box="true" data-live-search="true" multiple> <option>test</option>
</select>
但是当我删除 select (id="hotspotList" class="selectpicker" data-actions-box="true" data-live-search="true" ) 的选项时,它可以工作,似乎问题在小窝,但我需要它们
【问题讨论】:
-
.html(data["items"])呢?还是 json_decode(data) ? -
在收到“数据”时尝试记录它的内容。然后就很容易找到使用方法了……
-
@Random, I console.log(data.items);我得到: 跨度>
标签: javascript php ajax