【问题标题】:Setting Date format for JQuery date picker clears the initial date value为 JQuery 日期选择器设置日期格式会清除初始日期值
【发布时间】:2017-03-21 13:24:57
【问题描述】:

我正在尝试为日期选择器设置格式,但它会清除字段中的默认值。

$(".date").datepicker();
// While trying to format (the same format as mentioned above 'dd-mm-yy'), the field value is cleared on execution of below code
$('.date').datepicker("option", "dateFormat", "dd-mm-yy");
<html>

<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
</head>

<body>
  <!-- Kindly note that format of the value here is 'dd-mm-yy' -->
  <p>Date: <input class='date' type="text" value="31-12-2016"></p>
</body>

</html>

我已经阅读了这些帖子 |JQuery UI Datepicker date format issue with initial value|Why does constructing a JQuery UI Datepicker clear the field's value?|Setting 'option' on jqueryui DatePicker clears the textbox's value?

所有声明现有值应与我尝试设置的日期选择器格式匹配。看起来我很干净。

谁能帮帮我?谢谢。

【问题讨论】:

  • 能否在datepicker的初始化中设置日期格式而不是稍后?

标签: jquery jquery-ui jquery-ui-datepicker date-formatting


【解决方案1】:

它的工作!我在 datepicker 初始化时设置了日期格式。现在保留默认值,日期格式也可以使用。

@Twisty 感谢您对此进行说明

//Setting the format on initialisation
$(".date").datepicker({ dateFormat: 'dd-mm-yy' });

// While trying to format (the same format as mentioned above 'dd-mm-yy'), the field value is cleared on execution of below code

//$('.date').datepicker("option", "dateFormat", "dd-mm-yy");
<html>

<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
</head>

<body>
  <!-- Kindly note that format of the value here is 'dd-mm-yy' -->
  <p>Date: <input class='date' type="text" value="31-12-2016"></p>
</body>

</html>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-30
    • 1970-01-01
    • 1970-01-01
    • 2021-11-05
    相关资源
    最近更新 更多