如何使用javascript或jquery填充包含HTML文件的文件夹名称的选择选项表单。 [英] How do I fill a select option form with folder name which contains files in HTML using javascript or jquery.
问题描述
Hello World!我刚刚开始使用html进行网页编辑,我想使用javascript或jquery动态填充html中的select选项。实现这一目标的最佳方法是什么?我做了一些谷歌研究,但不幸的是这不起作用。我尝试了下面的链接中的示例,但它不适合我。我的代码中我做错了什么?我对其他建议持开放态度。
[ ^ ]
我尝试过:
Hello World! i'm just starting using html for web page and i want to populate the select option in html dynamically using javascript or jquery. What is the best way to achieve this. i make some google research, but this do not work unfortunaly. i tried it with the examples in the link below but it's not working for me. what i'm doing wrong in my code? I am open to other suggestions.
https://www.electrictoolbox.com/javascript-add-options-html-select/[^]
What I have tried:
<select class="col-xs-3" name="list" id="list">
<option value="" selected> Select List</option>
</select>
和我的JS看起来像:
and my JS look like:
<script language="javascript" type="text/javascript">
var select = document.getElementById("list");
select.options[select.options.length] = new Option('text 1', 'Value1');
</script>
我不知道如何在jquery中编码。欢迎任何想法。谢谢你的帮助。
i don't know how to code it in jquery. Any idea is welcome. thank you for your help.
推荐答案
试试这个
try this
var select = document.getElementById('list');
var items = [{ text: 'text1', value: 'value1' }, { text: 'text2', value: 'value2' }, { text: 'text3', value: 'value3' }];
var options = [];
for (var i = 0; i < items.length; i++)
options.push('<option value="' + items[i].text + '">' + items[i].text + '</option>');
select.innerHTML = options.join('');
演示: - JSFiddle [ ^ ]
这篇关于如何使用javascript或jquery填充包含HTML文件的文件夹名称的选择选项表单。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!