可能重复:
How to populate the options of a select element in javascript
我想在javascript中创建动态选择标记和选项标记。有什么建议?
答案 0 :(得分:7)
试 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);