대신이 작업을 수행 할 수 있습니다 :
http://jsfiddle.net/LXVhu/2/
은 스토어 그냥 변수에 DOM에 추가 된 요소는,이 변수를 채우는 함수를 호출합니다. 여전히를 바인드해야
// Instead of having to "click" the select fields
// I'd like to populate the <option> tags everytime a new select item is injected into the DOM.
$(document).on("click", "select.countrylist", function() {
var select = $(this);
$(countries).each(function(i) {
var option = $("<option></option>").attr("value", this.Key).text(this.Value.EnglishName);
if (this.Key === select.data('selected')) option.prop("selected", true);
select.append(option);
select.get().selectedIndex = i;
});
});
// populate all existing selects
$("select.countrylist").click();
// create new select and populate it
$('#test').click(function() {
var x = $('<select data-selected="USA" class="countrylist"></select>').appendTo("body");
// manually trigger a click which causes populating of select
x.click();
});
See this JSFiddle for a working example
참고 :
function populateSelect(elmt) {
var select = elmt;
$(countries).each(function(i) {
var option = $("<option></option>").attr("value", this.Key).text(this.Value.EnglishName);
if (this.Key === select.data('selected')) option.prop("selected", true);
select.append(option);
select.get().selectedIndex = i;
});
}
$('#test').click(function() {
// this select field will not be populated!
var elmt = $('<select data-selected="USA" class="countrylist"></select>').appendTo("body");
populateSelect(elmt);
});
이것은 약간의 장님이지만, [** load event **] (http://api.jquery.com/load-event/)는 어떻습니까? –
불행히도 작동하지 않았다. – doque
'select.append (option) .change (function() {var selectedIndex = $ (this) .children ('selected :)}.); '? – vladkras