jQuery วิธี Add option ให้ select box โดยดึงค่าจาก Array
ยกตัวอย่างเรามี select box ที่มี ID ชื่อ mySelect
เราสามารถ Add ค่า Option ให้กับ select box ด้วย jQuery ตามตัวอย่าง
var output = [];
selectValues = { "1": "test 1", "2": "test 2" };
$(document).ready(function(){
$.each(selectValues, function(key, value) {
output.push('');
});
$('#mySelect').html(output.join(''));
});
ตัวอย่างนี้จะถ่ายค่า option ลงไปใน array ก่อนแล้วค่อยนำมา add เข้าไปใน html ของ combobox อีกที