2011-04-23 10 views

답변

1
var selectM = document.createElement("select"), values = ["hi", "hello", "hola"]; 

for(var i = 0; i < values.length; ++i) { 
    var opt = document.createElement("option"); 
    opt.value = values[i]; 
    opt.text = values[i]; 
    selectM.add(opt, null);  
} 

// do stuff with your new select menu 
document.getElementById("someDiv").appendChild(selectM); 
0

당신이 jQuery를 사용하려면 ...

var arr = [1, 2, 3]; 
var $select = $('<select>'); 
for (var i = 0; i < arr.length; i++) { 
    var val = arr[i]; 
    $('<option>').val(val).text(val).appendTo($select); // This assumes your array is an primitive type array, if it's an object array you could use ex. val.Value 
} 

$select.appendTo($('... your selector here ...')); 
0

당신은 바닐라 JS과 jQuery를 갖고 있기 때문에, 여기 Mootools의를 통해 그것을 할 한 가지 방법입니다 1.3

var makeDropdown = function(vals) { 
    return new Element("select").adopt(vals.map(function(el) { 
     return new Element("option[value="+el+"][text="+el+"]"); 
    })); 
}; 


makeDropdown(["hi", "hello", "hola"]).inject(document.body); 
관련 문제