简体   繁体   English

为什么我的焦点无法正常工作?

[英]Why my focus is not working properly?

I have data.json file is being loaded after I focus but after I focus then my datepicker is not showing why I didn't understand. 我聚焦后有data.json文件正在加载,但是聚焦后我的日期选择器没有显示我不理解的原因。 but if I focus second time I can see.. where is my mistake ? 但是如果我第二次专注,我就能看到..我的错误在哪里?

 function flattenFieldsArray(arr) { return arr.map(function(item) { return item.field }) } $(function() { var focused = false; $(document.body).one("focus", '#checkin,#checkout', function() { if (!focused) { $.getJSON('data.json', function(data) { // use ajax data mapped to same structure as original variables var firstDate = flattenFieldsArray(data.firstDate); var lastDate = flattenFieldsArray(data.lastDate); var availabledays = flattenFieldsArray(data.availabledays); var booked = flattenFieldsArray(data.booked);; var ssdays = []; // nothing was changed below var dateFormat = "mm/dd/yy", from = $("#checkin").datepicker({ changeMonth: true, numberOfMonths: 2, firstDay: 1, minDate: new Date(firstDate), maxDate: new Date(lastDate), onSelect: function(selectedDate) { var yenitarih = new Date(); var date = $(this).datepicker('getDate'); date.setTime(date.getTime() + (1000 * 60 * 60 * 24)) $("#checkout").datepicker("option", "minDate", date); }, beforeShowDay: function(date) { var y = date.getFullYear().toString(); // get full year var m = (date.getMonth() + 1).toString(); // get month. var d = date.getDate().toString(); // get Day if (m.length == 1) { m = '0' + m; } // append zero(0) if single digit if (d.length == 1) { d = '0' + d; } // append zero(0) if single digit var currDate = y + '-' + m + '-' + d; if (jQuery.inArray(currDate, availabledays) >= 0) { return [false, "ui-highlight"]; } if (jQuery.inArray(currDate, booked) >= 0) { return [true, "ui-bos"]; } else { return [true]; } }, isTo1: true, }).on("change", function() { to.datepicker("option", "minDate", getDate(this)); }), to = $("#checkout").datepicker({ changeMonth: true, changeYear: true, firstDay: 1, numberOfMonths: 2, minDate: new Date(firstDate), maxDate: new Date(lastDate), onSelect: function(selectedDate) { $("#checkin").datepicker("option", "maxDate", selectedDate); }, beforeShowDay: function(date) { var y = date.getFullYear().toString(); // get full year var m = (date.getMonth() + 1).toString(); // get month. var d = date.getDate().toString(); // get Day if (m.length == 1) { m = '0' + m; } // append zero(0) if single digit if (d.length == 1) { d = '0' + d; } // append zero(0) if single digit var currDate = y + '-' + m + '-' + d; if (jQuery.inArray(currDate, booked) >= 0) { return [true, "ui-highlight-donus"]; } if (jQuery.inArray(currDate, availabledays) >= 0) { return [true, "ui-bos"]; } if (jQuery.inArray(currDate, ssdays) >= 0) { return [true, "ui-ss-donus"]; } else { return [true]; } } }).on("change", function() { from.datepicker("option", "maxDate", getDate(this)); }); }) focused = true; } }); }); 
 .form{ width:960px; margin:120px auto; } .form input{ width:250px; padding:10px; } .ui-highlight .ui-state-default{background: red !important;border-color: red !important;color: white !important; cursor:no-drop;} .ui-bos .ui-state-default{background: green !important;border-color: green !important;color: white !important;} .ui-ss .ui-state-default{background: yellow !important;border-color: yellow !important;color: gray !important; cursor:help;} .ui-ss-donus .ui-state-default{background: yellow !important;border-color: yellow !important;color: gray !important; cursor:help;} .ui-highlight-donus .ui-state-default{background: red !important;border-color: red !important;color: white !important; } .ui-testtarih .ui-state-default{ background:black !important; color:#FFF !important; } 
 <link data-require="jqueryui" data-semver="1.10.0" rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/jqueryui/1.10.0/css/smoothness/jquery-ui-1.10.0.custom.min.css" /> <div class="form"> <input type="text" id="checkin" /> <input type="text" id="checkout" /> <input type="submit" value="Search" /> </div> <script data-require="jquery" data-semver="3.1.1" src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <script data-require="jqueryui" data-semver="1.10.0" src="//cdnjs.cloudflare.com/ajax/libs/jqueryui/1.10.0/jquery-ui.js"></script> 

footnote: I couldn't add my json data to stackoverflow snippet that's why I add plunker demo in plunker demo works 脚注:我无法将我的json数据添加到stackoverflow片段中,这就是为什么我在plunker演示作品中添加了plunker演示

Click to see plunker demo 单击以查看插件演示

$.getJSON has a done() function in which you can write you logic after data is loaded. $.getJSON有一个done()函数,您可以在其中加载数据后编写逻辑。 You can trigger the datepicker manually inside the done function 您可以在完成函数中手动触发日期选择器

Here's a working fork 这是工作

It is because, on the first focus, the datepickers are applied on the elements and NOT triggered. 这是因为,首先要注意的是,日期选择器已应用于元素,并且未触发。 Mind the difference between binding an event and triggering it. 注意绑定事件和触发事件之间的区别。 On the first time focus, datepicker will be applied and NOT triggered as it is not already bound to the element. 在第一个时间焦点上,将应用datepicker且未触发它,因为它尚未绑定到该元素。 But on any subsequent focus, it would trigger the datepicker and open it since it is already bound to the element. 但是,在随后的任何焦点上,它将触发日期选择器并打开它,因为它已经绑定到该元素。

As a solution,you can try adding $("#checkin").datepicker('show'); 作为解决方案,您可以尝试添加$("#checkin").datepicker('show'); and $("#checkout").datepicker('show'); $("#checkout").datepicker('show'); after the end for $("#checkin").datepicker({}) and $("#checkout").datepicker({}) , respectively. $("#checkin").datepicker({})$("#checkout").datepicker({})之后。

You should load your data prior to the first focus event on your datepickers. 您应该在日期选择器上的第一个焦点事件之前加载数据。

Here's an example : 这是一个例子:

plunkr 朋克

$( document ).ready(function() {
    var data;
    $.getJSON('data.json', function (d) {
        data = d;
        console.log('data', d);
        setupDatepickers();
    });

    function setupDatepickers() {
        // use ajax data mapped to same structure as original variables
                 var firstDate = flattenFieldsArray(data.firstDate);
                 var lastDate = flattenFieldsArray(data.lastDate);
                 var availabledays = flattenFieldsArray(data.availabledays);
                 var booked = flattenFieldsArray(data.booked);;
                 var ssdays = [];

                 // nothing was changed below

                 var dateFormat = "mm/dd/yy",
                     from       = $("#checkin")
                         .datepicker({
                             beforeShowDay : function (date) {
                                 var y = date
                                     .getFullYear()
                                     .toString(); // get full year
                                 var m = (date.getMonth() + 1).toString(); // get month.
                                 var d = date
                                     .getDate()
                                     .toString(); // get Day
                                 if (m.length == 1) {
                                     m = '0' + m;
                                 } // append zero(0) if single digit
                                 if (d.length == 1) {
                                     d = '0' + d;
                                 } // append zero(0) if single digit
                                 var currDate = y + '-' + m + '-' + d;
                                 if (jQuery.inArray(currDate, availabledays) >= 0) {
                                     return [false, "ui-highlight"];
                                 }

                                 if (jQuery.inArray(currDate, booked) >= 0) {
                                     return [true, "ui-bos"];
                                 } else {
                                     return [true];
                                 }

                             },
                             changeMonth   : true,
                             firstDay      : 1,
                             isTo1         : true,
                             maxDate       : new Date(lastDate),
                             minDate       : new Date(firstDate),
                             numberOfMonths: 2,
                             onSelect      : function (selectedDate) {
                                 var yenitarih = new Date();

                                 var date = $(this).datepicker('getDate');
                                 date.setTime(date.getTime() + (1000 * 60 * 60 * 24))

                                 $("#checkout").datepicker("option", "minDate", date);
                             }
                         })
                         .on("change", function () {
                             to.datepicker("option", "minDate", getDate(this));
                         }),
                     to         = $("#checkout")
                         .datepicker({
                             beforeShowDay : function (date) {
                                 var y = date
                                     .getFullYear()
                                     .toString(); // get full year
                                 var m = (date.getMonth() + 1).toString(); // get month.
                                 var d = date
                                     .getDate()
                                     .toString(); // get Day
                                 if (m.length == 1) {
                                     m = '0' + m;
                                 } // append zero(0) if single digit
                                 if (d.length == 1) {
                                     d = '0' + d;
                                 } // append zero(0) if single digit
                                 var currDate = y + '-' + m + '-' + d;

                                 if (jQuery.inArray(currDate, booked) >= 0) {
                                     return [true, "ui-highlight-donus"];
                                 }

                                 if (jQuery.inArray(currDate, availabledays) >= 0) {
                                     return [true, "ui-bos"];
                                 }

                                 if (jQuery.inArray(currDate, ssdays) >= 0) {
                                     return [true, "ui-ss-donus"];
                                 } else {
                                     return [true];
                                 }
                             },
                             changeMonth   : true,
                             changeYear    : true,
                             firstDay      : 1,
                             maxDate       : new Date(lastDate),
                             minDate       : new Date(firstDate),
                             numberOfMonths: 2,
                             onSelect      : function (selectedDate) {
                                 $("#checkin").datepicker("option", "maxDate", selectedDate);
                             }
                         })
                         .on("change", function () {
                             from.datepicker("option", "maxDate", getDate(this));
                         });

    }
});

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

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