問(wèn)題描述
在編寫Web頁(yè)面的時(shí)候常常需要在后端取出數(shù)據(jù)動(dòng)態(tài)放入select標(biāo)簽中,以供選擇。
解決辦法
在HTML代碼段中只需寫入
<select id="select"><option value="-1">--請(qǐng)選擇--</option></select>
在JavaScript代碼段中寫入以下ajax取數(shù)據(jù)并綁定數(shù)據(jù)的過(guò)程
$.ajax({ type : "post", url : "api/department/list", //此次url改為真正需要的url success : function(data, status) { $.each(data, function(index, item) { $("#select").append( //此處向select中循環(huán)綁定數(shù)據(jù) "<option value="+item.id+">" + item.name+ "</option>"); }); },});
最后可通過(guò)javaScript語(yǔ)句document.getElementById("select").value獲取select標(biāo)簽中被選中的值。
以上這篇通過(guò)Ajax方式綁定select選項(xiàng)數(shù)據(jù)的實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選