在 Javascript 中创建一个动态选择对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11543427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 13:36:28 来源:igfitidea点击:
Create a dynamic select object in Javascript
提问by Pranay Rana
Possible Duplicate:
How to populate the options of a select element in javascript
I would like to create a dynamic select tag and options tag in javascript. Any suggestions?
我想在 javascript 中创建一个动态选择标签和选项标签。有什么建议?
回答by Pranay Rana
try javascript
试试javascript
var select = document.createElement( 'select' );
var option;
var inputdata = 123||456||789;
inputdata.split( '||' ).forEach(function( item ) {
option = document.createElement( 'option' );
option.value = option.textContent = item;
select.appendChild( option );
});
jquery
查询
var inputdata = 123||456||789;
var split = input.split('||');
var select = $('<select name="options" id="options"></select>');
$.each(split, function(index, value) {
var option = $('<option></option>');
option.attr('value', value);
option.text(value);
select.append(option);
});
$('#container).empty().append(select);