我有一个jquery datepicker,它通过局部视图在ajax请求中动态添加。 我必须在选择日期后进行js检查,所以我想出了这个 -
$(document).ready(function(){
$(document).on('change', 'input[name="ReportDate"]', function () { if ($(this).attr('data-isDccmAndReady') == 'True') { var d = new Date(); var currentDate = (d.getMonth() + 1) + "/" + +d.getDate() + "/" + d.getFullYear(); var dateSelected = $(this).val(); if (new Date(dateSelected) > new Date(currentDate)) { var answer = confirm("The contract has been already reported, are you sure you wish to continue?"); if (answer) { return true; } else { //this is correctly resetting the value $(this).val($(this).attr('data-originalReportDateVal'));//$(this).val() gives the old and the correct value. return false; } } } return false; }); }); </script>
当您在确认框中选择取消时,datepicker值不会恢复为html中的旧值或不刷新。
答案 0 :(得分:0)
http://api.jqueryui.com/datepicker/#method-refresh
if (new Date(dateSelected) > new Date(currentDate)) {
var answer = confirm("The contract has been already reported, are you sure you wish to continue?");
if (answer) {
return true;
} else {
$(this).val($(this).attr('data-originalReportDateVal'));
$(this).datepicker( "refresh" ); // <-- Try this (or with the right datepicker selector)
return false;
}
}
答案 1 :(得分:0)
这适用于解决方法
var id = $(this).attr('id');
$('#'+ id).val($(this).attr('data-originalReportDateVal'));
我不确定为什么它不能直接与
一起使用$(本).VAL($(本).attr( '数据originalReportDateVal'));