[英]Trying to create dynamic select boxes with Javascript and jQuery
I'm not a front-end developer and I'm trying to create a select box and add its options totally dynamically using Javascript and jQuery. 我不是前端开发人员,而是尝试创建一个选择框并使用Javascript和jQuery完全动态地添加其选项。
I'm almost there but I am having a problem adding each new select to the div block (the number of selects is different at different times). 我快到了,但是在将每个新选择添加到div块时遇到了问题(选择的次数在不同时间是不同的)。
So far, I can either get the last select created in the following code displayed (but without span tags) 到目前为止,我可以在显示的以下代码中获得创建的最后一个选择(但没有span标签)
$('#meeting-eft-time-unit-data').append('<span>').html(se).append('</span>');
or I just get the following list ... 或者我只得到以下列表...
$('#meeting-eft-time-unit-data').append('<span>' + se + '</span>');
object HTMLSelectElement][object HTMLSelectElement][object HTMLSelectElement][object HTMLSelectElement][object HTMLSelectElement]
Here's the code: 这是代码:
$.each(event_type_time_units,
function(index, optionData) {
var se = document.createElement("select");
// give it a name from 'name'
se.name = 'eft[' + optionData.name + ']';
// populate it from 'range_from .. range_to
for(var i=optionData.range_from; i <= optionData.range_to; i++) {
var option = new Option(i, i);
if ($.browser.msie) {
se.add(option);
} else {
se.add(option,null);
}
}
// add display name to a label in a div block
$('#meeting-eft-time-unit-headers').append('<span>' + optionData.display_name + '</span>');
// add select to a div block
/**
This gives me the a good select but only the last one processed is displayed
$('#meeting-eft-time-unit-data').append('<span>').html(se).append('</span>');
or
This gives the text as explained above
$('#meeting-eft-time-unit-data').append('<span>' + se + '</span>');
**/
});
尝试:
$('#meeting-eft-time-unit-headers').append($('<span/>').append(se));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.