我的script代碼如下,
function getleve(){ var levelObj = document.getElementById("leve"); var pfObj = document.getElementById("PF"); var levelValue = levelObj.value; pfObj.innerHTML=''; $.get('petTypeServlet?action=pf',{id:levelValue},function(data){ }); }然後這是兩個下拉列表
<select name="leve" id="leve" onchange="getleve();">
<option value="2">2</option>
<option value="3">3</option>
</select>
<td align="left"><select name="PF" id="PF" >
</select>
後台返回數據格式如下:
[{"PtID":1,"PtName":"陸上寵物","PtLevel":1,"PtFid":0},{"PtID":9,"PtName":"海洋寵物","PtLevel":1,"PtFid":0},{"PtID":10,"PtName":"空中寵物","PtLevel":1,"PtFid":0}]
請問怎麼把下拉列表值設為PtID,顯示為PtName,謝謝大家
調用jquery的map方法組合成option
function getleve() {
var levelObj = document.getElementById("leve");
var pfObj = document.getElementById("PF");
var levelValue = levelObj.value;
pfObj.innerHTML = '';
$.get('petTypeServlet?action=pf', { id: levelValue }, function (data) {
if (typeof data == 'string') data = eval('(' + data + ')'); //$.get有可能會依據響應頭轉換對象,所以判斷下類型
$(pfObj).html($(data).map().get(function () { return '<option value="' + this.PtID + '">' + this.PtName + '</option>' }).join(''));
});
}