2010-01-20 141 views
4

我有一個選擇元素與幾個日期,但我也想給datepicker選擇一個日期的可能性,所以我有以下代碼顯示日曆圖標旁邊選擇字段。'選擇'元素的jQuery ui.datepicker

<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,添加一個新的選項,選擇HTML元素?你不知道它是什麼問題嗎?

謝謝。

更新1:

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

運行完美,只是此話,我需要標記爲選擇新的選項,只需編輯,如:$('<option selected/>')

回答

3

除非他們是你的一些功能,我米真的不知道什麼.add()getElementsByNameByName()是。我似乎無法在jQuery api中找到add

而且,使用jQuery小部件工作時,我更喜歡使用jQuery選擇:

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

爲我工作。

+0

非常感謝您根據我的需要完美地工作。 – framara 2010-01-21 09:59:38