' select'上的jQuery ui.datepicker元素 [英] jQuery ui.datepicker on 'select' element
问题描述
我有一个带有几个日期的select元素,但我也想提供从日期选择器中选择日期的可能性,所以我有以下代码在select字段旁边显示一个日历图标.
I have a select element with a couple of dates but I also want to give the possibility of picking a date from the datepicker so I have the following code that displays a calendar icon next to the select field.
<select name="birthday" >
<option value="${merge0.birthday}">${merge0.birthday}</option>
<option value="${merge1.birthday}">${merge1.birthday}</option>
</select>
<input type="hidden" id="bday_icon" />
然后,这是我的日期选择器脚本
Then, this is my datepicker script
$("#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元素中添加一个新选项吗?你看不出什么问题吗?
Shouldn't the function onSelect, add a new option to the select html element? can't you see what it's wrong?
谢谢.
更新1:
onSelect: function(dateText, inst) {
var opt = $('<option />').attr('value', dateText).text(dateText);
$('select[name=birthday]').append(opt);
}
效果完美,只需指出我需要将新选项标记为选中即可,因此只需进行如下编辑即可:$('<option selected/>')
Works perfect, only remark that I needed to mark as selected the new option so just edit like: $('<option selected/>')
推荐答案
除非它们是您的某些功能,否则我不太确定.add()
和getElementsByNameByName()
是什么.我似乎在jQuery API中找不到add
.
Unless they're some of your functions, I'm not really sure what .add()
and getElementsByNameByName()
are. I can't seem to find add
in the jQuery api.
此外,在使用jQuery小部件时,我更喜欢使用jQuery选择器:
Also, when working with jQuery widgets, I prefer to use jQuery selectors:
onSelect: function(dateText, inst) {
var opt = $('<option />').attr('value', dateText).text(dateText);
$('select[name=birthday]').append(opt);
}
为我工作.
这篇关于&#39; select&#39;上的jQuery ui.datepicker元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!