4

我有一个带有几个日期的选择元素,但我也想提供从日期选择器中选择日期的可能性,所以我有以下代码,在选择字段旁边显示一个日历图标。

<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/>')

4

1 回答 1

3

除非它们是您的一些功能,否则我不确定是什么.add()getElementsByNameByName()是。我似乎无法add在 jQuery api 中找到。

此外,在使用 jQuery 小部件时,我更喜欢使用 jQuery 选择器:

onSelect: function(dateText, inst) {
    var opt = $('<option />').attr('value', dateText).text(dateText);
    $('select[name=birthday]').append(opt);
}

为我工作。

于 2010-01-20T18:53:36.077 回答