简体   繁体   English

文本框onChange事件和Datepicker onSelect冲突

[英]Textbox onChange event and Datepicker onSelect conflict

I have two text boxes for date fields. 我有两个用于日期字段的文本框。 User is allowed to enter date in those fields via both jQuery-ui Datepicker and via manually typing through keyboard. 允许用户通过jQuery-ui Datepicker和通过键盘手动键入在这些字段中输入日期。 The requirement is after entering date in the 1st textbox, the second textbox should get populated with the date exactly 1 year later.I have handled most of the scenarios. 要求是在第一个文本框中输入日期后,第二个文本框应在一年后恰好填充日期。我已经处理了大多数情况。 I will try to explain clearly in which scenario I am facing the issue. 我将尝试清楚地解释在哪种情况下我会遇到此问题。 Suppose I am typing in the date manually from keyboard and suddenly I decide to select a different date from Datepicker instead. 假设我是通过键盘手动输入日期,然后突然决定从Datepicker中选择一个不同的日期。 In this case the the textbox is not showing the selected date from datepicker. 在这种情况下,文本框不会显示从日期选择器中选择的日期。 I hope I am able to articulate clearly. 我希望我能说清楚。 My guess is Datepicker's onSelect() event is not getting fired in this particular scenario because of any conflict with the textbox's onChange() event. 我的猜测是,由于与文本框的onChange()事件发生任何冲突,在这种特定情况下Datepicker的onSelect()事件没有被触发。

 $('#datepicker1').datepicker({ constrainInput: "true", dateFormat: "mm/dd/yy", changeMonth: true, changeYear: true, onSelect: function (date) { var parsedDate = parseDate(date); var moddate = new Date(Date.parse(parsedDate)); moddate.setFullYear(moddate.getFullYear() + 1); var newDate = moddate.toDateString(); newDate = new Date(Date.parse(newDate)); $("#datepicker2").datepicker("option", "maxDate", newDate); $('#datepicker2').datepicker('setDate', newDate); } }); $('#datepicker2').datepicker({ dateFormat: "mm/dd/yy", changeMonth: true, changeYear: true, }); var maskConfig = { leapday: "29-02-", separator: "/", showMaskOnHover: false, showMaskOnFocus: false, placeholder: "00/00/0000" } $('#datepicker1').inputmask('mm/dd/yyyy', maskConfig); $('#datepicker2').inputmask('mm/dd/yyyy', maskConfig); $('#datepicker1').on("change",function () { var parsedDate = parseDate($('#datepicker1').val()); var date = new Date(Date.parse(parsedDate)); date.setFullYear(date.getFullYear() + 1); var newDate = date.toDateString(); newDate = new Date(Date.parse(newDate)); $("#datepicker2").datepicker("option", "maxDate", newDate); $('#datepicker2').datepicker('setDate', newDate); }); function parseDate(input) { var parts = input.split('/'); return new Date(parts[2], parts[0] - 1, parts[1]); } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.inputmask/3.3.4/jquery.inputmask.bundle.min.js"></script> <input type="text" id="datepicker1"/> <input type="text" id="datepicker2"/> 

I couldn't find any exact fix for this issue. 我找不到此问题的确切解决方法。 I am currently going with a small hack/workaround. 我目前正在使用小型hack /解决方法。 I am hiding the datepicker as soon as user starts typing inside the textbox. 用户开始在文本框中输入内容时,我将隐藏日期选择器。 I am using the "keyup" event handler. 我正在使用“ keyup”事件处理程序。

 $('#datepicker1').datepicker({ constrainInput: "true", dateFormat: "mm/dd/yy", changeMonth: true, changeYear: true, onSelect: function (date) { var parsedDate = parseDate(date); var moddate = new Date(Date.parse(parsedDate)); moddate.setFullYear(moddate.getFullYear() + 1); var newDate = moddate.toDateString(); newDate = new Date(Date.parse(newDate)); $("#datepicker2").datepicker("option", "maxDate", newDate); $('#datepicker2').datepicker('setDate', newDate); } }); $('#datepicker2').datepicker({ dateFormat: "mm/dd/yy", changeMonth: true, changeYear: true, }); var maskConfig = { leapday: "29-02-", separator: "/", showMaskOnHover: false, showMaskOnFocus: false, placeholder: "00/00/0000" } $('#datepicker1').inputmask('mm/dd/yyyy', maskConfig); $('#datepicker2').inputmask('mm/dd/yyyy', maskConfig); $('#datepicker1').on("change",function () { var parsedDate = parseDate($('#datepicker1').val()); var date = new Date(Date.parse(parsedDate)); date.setFullYear(date.getFullYear() + 1); var newDate = date.toDateString(); newDate = new Date(Date.parse(newDate)); $("#datepicker2").datepicker("option", "maxDate", newDate); $('#datepicker2').datepicker('setDate', newDate); }); $('#datepicker1').on('keyup', function() { if (this.value.length > 0) { $('#datepicker1').datepicker('hide') ; } else{ $('#datepicker1').datepicker('show') ; } }); $('#datepicker2').on('keyup', function() { if (this.value.length > 0) { $('#datepicker2').datepicker('hide') ; } else{ $('#datepicker2').datepicker('show') ; } }); function parseDate(input) { var parts = input.split('/'); return new Date(parts[2], parts[0] - 1, parts[1]); } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.inputmask/3.3.4/jquery.inputmask.bundle.min.js"></script> <input type="text" id="datepicker1"/> <input type="text" id="datepicker2"/> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM