admin管理员组文章数量:1295315
I have a select box:
<select id="translationOptions" name="translationOptions"></select>
And I have a js array
var translationOptions = ["Egyptian Hieroglyphs", "Al Bhed (Final Fantasy X)", "Futurama"];
How would I auto populate the select box based on the var translationOptions
?
I have a select box:
<select id="translationOptions" name="translationOptions"></select>
And I have a js array
var translationOptions = ["Egyptian Hieroglyphs", "Al Bhed (Final Fantasy X)", "Futurama"];
How would I auto populate the select box based on the var translationOptions
?
3 Answers
Reset to default 4$.each(translationOptions, function(key, value) {
$('#mySelect')
.append($("<option></option>")
.attr("value",key)
.text(value));
});
What is the best way to add options to a select from an array with jQuery?
$.each(translationOptions, function(index, value) {
$('#translationOptions').append($("<option />").val(index).text(value));
});
This uses text for display and index in the array for value.
You can create options dynamically and append to select box in this way as well.
jQuery.each(translationOptions, function(key, value) {
jQuery('<option/>', {
'value': key,
'text' : value
}).appendTo('#translationOptions');
本文标签: Auto populate select box using Javascript and jQueryStack Overflow
版权声明:本文标题:Auto populate select box using Javascript and jQuery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741610654a2388239.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论