我正在使用一个优秀的 jquery 插件来为我的网络表单选择生日日期。
演示在这里: http://abecoffman.com/stuff/birthdaypicker/
我正在处理的问题是表单验证。我的表单验证重定向回同一页面,我将丢失选择的日期。
有没有办法向日期选择器的 javascript 添加一个选项: http://abecoffman.com/stuff/birthdaypicker/bday-picker.js
这样我就可以设置一个“默认选择日期”。配置可以像这样工作:
$("#picker1").birthdaypicker({
chosenDay: 1;"
chosenMonth: 28;"
chosenYear: 2011;"
});
这会将“选定”日期设置为 2011 年 1 月 28 日。
最佳答案
无需修改插件,您可以使用 JQuery 来设置值。插件生成的标记如下所示:
<fieldset class='birthday-picker'>
<select class='birth-year' name='birth[year]'></select>
<select class='birth-month' name='birth[month]'></select>
<select class='birth-day' name='birth[day]'></select>
</fieldset>
因此您的 JQuery 只需要获取这些元素和 set their value .您需要确保设置的值存在于选择列表中。
$('select.birth-year').val('my_year'); // 1980
$('select.birth-month').val('my_month'); // 1-12 (not "Jan" which is the text)
$('select.birth-day').val('my_day'); // 1-31 (keep month limitation in mind)
此代码必须在调用插件后调用,否则标记还不会生成。
关于javascript - 处理表单刷新的 jQuery 生日选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4830758/