我正在使用jQuery加载一个Option元素,当我尝试设置所选的选项时,它不起作用。有人可以告诉我需要修改它来修复它。
HTML
<select data-placeholder="Program View" class="chosen-select"></select>
的jQuery
$.getJSON(path)
.done(function (data) {
if (!data) {
return
}
setTimeout(
function () {
var loadedAtLeastOneProgram = false;
$.each(data, function (i, val) {
if (val.Value.toLowerCase() != "rt") {
loadedAtLeastOneProgram = true;
$('.chosen-select').append('<option value="' + val.Value + '">' + val.Value + '</option>')
}
})
$('.chosen-select').trigger('chosen:updated');
if (!loadedAtLeastOneProgram) {
$("#programsDataItems").append("<li><a href='#'>No Tailored Programs Available</a></li>");
}
// After loading (or reloading) the programs, see if one was previously selected. The user may have refreshed their browser.
// In another part of my application, I set the "CurrentProgram"
var program = retrieveStorageItem("CurrentProgram");
if (program == null || program == undefined) {
program = "rt";
} else {
// ***** This is the part that does not work. Nothing shows as selected. ******
$(".chosen-select").val(program).prop('selected', true);
}
}, 50);
})
.fail(function (e) {
// Adding logging for errors.
});
答案 0 :(得分:0)
$('.chosen-select').trigger('chosen:updated');
这是您需要触发以应用选择的内容。
else {
// ***** This is the part that does not work. Nothing shows as selected. ******
$(".chosen-select").val(program).prop('selected', true);
$('.chosen-select').trigger('chosen:updated'); //added
}