为什么构建JQuery UI Datepicker会清除字段的值? [英] Why does constructing a JQuery UI Datepicker clear the field's value?

查看:50
本文介绍了为什么构建JQuery UI Datepicker会清除字段的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个INPUT标签,其value属性设置为15-May-2012,我用它初始化了一个JQuery UI Datepicker,如下所示:

I have an INPUT tag with the value attribute set to "15-May-2012" and I have initialized a JQuery UI Datepicker with it as follows:

<input type="text" name="schedule" id="schedule"
      value="15-May-2012">

<script type="text/javascript">
  $(function() {
    $( "#schedule" ).datepicker();
    $( "#schedule" ).datepicker( "option", "dateFormat", "d-M-yy" );
  });
</script>

出于某种原因,一旦初始化Datepicker,INPUT标签的值就会被清除。请注意,INPUT字段中的值与Datepicker设置的格式相匹配。

For some reason, the value of the INPUT tag is being cleared once the Datepicker is initialized. Note that the value in the INPUT field matches the format set for the Datepicker.

为什么值被清除?

以下是它的实例: http://agnelkurian.com/lab/datepicker_example.html

推荐答案

绑定datepicker时:

When you bind the datepicker:

$('#schedule').datepicker();

它将使用默认 dateFormat ,即mm / dd / yy;您的15-May-2012与该格式不匹配,因此当使用mm / dd /无法解析时,datepicker会将其清除YY。然后你改变格式:

it will use the default dateFormat and that is "mm/dd/yy"; your "15-May-2012" doesn't match that format so the datepicker clears it when it fails to parse it using "mm/dd/yy". Then you change the format:

$("#schedule").datepicker("option", "dateFormat", "d-M-yy");

已经丢失。

绑定datepicker时应设置格式:

You should set the format when you bind the datepicker:

$("#schedule").datepicker({
    dateFormat: 'd-M-yy'
});

演示: http://jsfiddle.net/ambiguous/mHyn7/

这篇关于为什么构建JQuery UI Datepicker会清除字段的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆