javascript – jQuery生日选择器处理表单刷新

前端之家收集整理的这篇文章主要介绍了javascript – jQuery生日选择器处理表单刷新前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用一个优秀的 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)

调用插件后必须调用代码,否则将不会生成标记.

猜你在找的jQuery相关文章