大脑不能正常工作。有人帮我填补这里的空白。jquery,javascript,更新焦点下拉列表选项,但保留选定的值
需要选择当前选定的值,重新填充选项,然后选择之前的选择(如果它仍在列表中)。
为了方便起见,新值和先前选定的值将具有相同的名称属性,假定它在更新后仍在列表中,并且名称对于选项列表将始终是唯一的。
//assume Options is a globally defined var for this example with format:
// [{"display":"something", "value": "something's value"}, etc.. ]
function LazyLoadOptionsIntoSelect($select, options)
{
//get current option
//repopulate options
$select.html("");
$("<option>").text("--Select a File--")
.appendTo($select);
$.each(options, function()
{
var item = this;
$("<option>").attr("name", function() { return item.display; })
.val(function() { return item.value; })
.text(item.display)
.appendTo($select);
});
//select previously selected option if still in list
}
$(".lazyloadedselect", "#context").live("focus", function()
{
LazyLoadOptionsIntoSelect($(this), Options);
});
编辑:错在我的代码,无关的问题,但仍然是错误的
huuuuuuh ..... hm,我的工作不太好! – 2011-06-09 16:09:15