答案 0 :(得分:3)
错误未显示,因为parseDate()
函数发生错误,因为日期无效。您需要先处理该错误。这是jsFiddle
$(function() {
$("#datepicker").datepicker({
dateFormat: 'dd/mm/yy',
});
$('button').click(function() {
$('#error').text('');
try {
var dateParse = $.datepicker.parseDate("dd/mm/yy", $("#datepicker").val());
} catch (e) {}
if (dateParse) {
$('#error').text(dateParse);
} else {
$('#error').text('invalid date format');
}
});
});
<link href="https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<p>Date:
<input type="text" id="datepicker">
</p>
<p id="error">
</p>
<button>
Validate
</button>
我建议显示错误而不是您的自定义:
$(function() {
$("#datepicker").datepicker({
dateFormat: 'dd/mm/yy',
});
$('button').click(function() {
$('#error').text('');
try {
var dateParse = $.datepicker.parseDate("dd/mm/yy", $("#datepicker").val());
if (dateParse) {
$('#error').text(dateParse);
}
} catch (e) {$('#error').text(e);}
});
});
<link href="https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<p>Date:
<input type="text" id="datepicker">
</p>
<p id="error">
</p>
<button>
Validate
</button>