通過(guò)Ajax方式綁定select選項(xiàng)數(shù)據(jù)的實(shí)例
來(lái)源:懂視網(wǎng)
責(zé)編:小采
時(shí)間:2020-11-27 22:51:30
通過(guò)Ajax方式綁定select選項(xiàng)數(shù)據(jù)的實(shí)例
通過(guò)Ajax方式綁定select選項(xiàng)數(shù)據(jù)的實(shí)例:問(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&g
導(dǎo)讀通過(guò)Ajax方式綁定select選項(xiàng)數(shù)據(jù)的實(shí)例:問(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&g

問(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)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com
通過(guò)Ajax方式綁定select選項(xiàng)數(shù)據(jù)的實(shí)例
通過(guò)Ajax方式綁定select選項(xiàng)數(shù)據(jù)的實(shí)例:問(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&g