我正在尝试设置日期选择器的格式,但它会清除字段中的默认值。
$(".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?
所有声明现有值应与我正在尝试设置的日期选择器格式匹配。看起来我很干净。
任何人都可以帮助我吗?感谢。
答案 0 :(得分:0)
它的工作!我在初始化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>