如果你
为您的选择添加一个类:
<select title="" id="" class="initMySelect"> </select>
定位类以重新填充选择
来自对象的示例:
var object={
"6db01de6-a1e8-4ea6-bf01-4562b56468b9": {
"UID": "6db01de6-a1e8-4ea6-bf01-4562b56468b9",
"name": "aa",
"description": "aa"
},
"284c3172-268a-4342-d3f0-d00fafd3d482": {
"UID": "284c3172-268a-4342-d3f0-d00fafd3d482",
"name": "bb",
"description": "bb"
},
"b124f4df-6caa-43e8-eb97-536076b4832b": {
"UID": "b124f4df-6caa-43e8-eb97-536076b4832b",
"name": "cc",
"description": "cc"
},
"c934634a-0775-41bd-d72a-d8900ebcbdd1": {
"UID": "c934634a-0775-41bd-d72a-d8900ebcbdd1",
"name": "dd",
"description": "dd"
},
"fb5b8dcb-b9fb-405d-9fcf-3f551727459a": {
"UID": "fb5b8dcb-b9fb-405d-9fcf-3f551727459a",
"name": "ee",
"description": "ee"
},
"a98f3449-bb55-46e3-b9ce-f9e2dd6d74a9": {
"UID": "a98f3449-bb55-46e3-b9ce-f9e2dd6d74a9",
"name": "ff",
"description": "ff"
}
}
function initMySelect(value) {
var options = "";
var selected = "";
$.each(object, function(k, v) {
if (value === v.UID) {
selected = 'selected = "selected"';
} else {
selected = "";
}
options += '<option ' + selected + ' value=' + v.UID + '>' + v.name + '</option>';
});
$('.initMySelect').html(options);
}
- 无论您进行什么 CRUD,都会重新初始化该类,这意味着该类将保存所有最新更改。所以在任何添加,更新,删除,exe你的功能之后
initMySelect();
查看您的选择时,选定的索引仍将存在,并且您所做的额外 Crud 更改将出现在您的选择中。4. 在按钮上添加事件侦听器的功能,将选定的值发送到 initMySelect
var el = document.getElementById("repopulateSelect");
el.addEventListener("click", function() {
initMySelect(document.getElementById("selectTest").value);
}, false);
JSFiddle 链接: