我有一个带有几个日期的选择元素,但我也想提供从日期选择器中选择日期的可能性,所以我有以下代码,在选择字段旁边显示一个日历图标。
<select name="birthday" >
<option value="${merge0.birthday}">${merge0.birthday}</option>
<option value="${merge1.birthday}">${merge1.birthday}</option>
</select>
<input type="hidden" id="bday_icon" />
然后,这是我的日期选择器脚本
$("#bday_icon").datepicker(
{
changeMonth: true,
changeYear: true,
showOn: 'button',
buttonImage: 'cal/images/calendar.gif',
buttonImageOnly: true,
onSelect: function(dateText, inst) {
var field = document.getElementsByNameByName("birthday");
var opt = document.createElement('option');
opt.text = dateText;
opt.value = dateText;
field.add(opt, null);
}
});
函数 onSelect 不应该为 select html 元素添加一个新选项吗?你看不出有什么问题吗?
谢谢。
更新1:
onSelect: function(dateText, inst) {
var opt = $('<option />').attr('value', dateText).text(dateText);
$('select[name=birthday]').append(opt);
}
工作完美,只是说我需要标记为选中的新选项,所以只需编辑如下:$('<option selected/>')