「jQuery入門」selectタグにoptionタグを追加するサンプルコード

JSコード:
var stationName = {
tamati: '田町’,
ooimati: '大井町’,
shinagawa: '品川’,
oosaki: '大崎’,
shibuya: '渋谷’,
shinnjuku: '新宿’
},
$select = $('.stationSelect’),
$option,
isSelected;

$.each(stationName, function (value, name) {
isSelected = (value === 'shibuya’ || value === 'shinnjuku’);
$option = $('<option>’)
.val(value)
.text(name)
.prop('selected’, isSelected);
$select.append($option);
});

JavaScript

Posted by arkgame