[英]Create select field dynamically - >selected< option doesn't show up
I'm writing a function for dynamic creation of forms with jQuery. 我正在编写一个使用jQuery动态创建表单的函数。 Now I stuck at due the selected parameter won't show up by any reason.
现在我坚持认为,由于任何原因所选参数都不会显示。 Here's a Fiddle .
这是小提琴 。
I don't assume this to be a big deal, however I got stuck. 我不认为这有什么大不了,但是我被卡住了。
// Creates select element with N options.
// options is an array with options
// name is the elements ID as a string
// selected, optionally, is the selected option. must be the same type as in options
function make_dynamic_select( options, name, selected ){
select = "<select id='' type='' size ='1', name='" + name + "'>"
options.forEach(function(option){
// a little debug
console.log(typeof option,option," === ",typeof selected,selected,(option === selected))
// append the option
select += (option === selected)? "<option selected>":"<option>" + option + "</option>"
});
return select += "</select>"
};
function make_dynamic_select( options, name, selected ){
select = "<select id='' type='' size ='1', name='" + name + "'>"
options.forEach(function(option){
console.log(typeof option,option," === ",typeof selected,selected,(option === selected))
console.log(option === selected)
select += (option === selected)? "<option selected>"+option+"</option>":"<option>" + option + "</option>"
})
select += "</select>"
return select
};
$("#foo").append(make_dynamic_select([1,2,3],"bar",2));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.