简体   繁体   English

如何在jquery UI datepicker中创建日期范围,而又不在主datepicker函数中再次调用datepicker?

[英]How to create a date range in jquery UI datepicker without calling datepicker again inside the main datepicker function?

I have a weird problem with jquery UI datepicker when choosing range. 选择范围时,jquery UI datepicker有一个奇怪的问题。 Before the question, I am working on code written by lots of other people. 在提出问题之前,我正在研究许多其他人编写的代码。 They have included jquery Tools and UI in optimized order so all the functions work. 它们以优化的顺序包括了jquery工具和UI,因此所有功能都可以使用。 All except jquery UI. 除jquery UI外的所有内容。 The reason I mentioned is, I cant update the versions or switch around the order of the libraries because it breaks all the other codes :( (But I did make sure jquery, js, jquery UI is not included twice anywhere) 我提到的原因是,我无法更新版本或切换库的顺序,因为它破坏了所有其他代码:((但我确保jquery,js,jquery UI不会在任何地方包含两次)

That said, the problem is this - I want to have two dates where the 'to' date should be later than the 'from' date. 就是说,问题是这样的-我想有两个日期,其中“到”日期应该晚于“从”日期。 This should be easy with the following code from jquery site: 使用来自jquery网站的以下代码,这应该很容易:

$(function() {
    $( "#from" ).datepicker({
        defaultDate: "+1w",
        changeMonth: true,
        numberOfMonths: 3,
        onClose: function( selectedDate ) {
            $( "#to" ).datepicker( "option", "minDate", selectedDate ); //BREAKS!
        }
    });
    $( "#to" ).datepicker({
        defaultDate: "+1w",
        changeMonth: true,
        numberOfMonths: 3,
        onClose: function( selectedDate ) {
            $( "#from" ).datepicker( "option", "maxDate", selectedDate ); //BREAKS!
        }
    });
});

The issue is the code breaks when I choose the date at the lines where I have commented as 'BREAKS!'. 问题是当我在注释为“ BREAK!”的行中选择日期时,代码中断。 It is simply not detecting the 2nd datepicker inside the main datepicker function and giving me error - SCRIPT438: Object doesn't support property or method 'datepicker' 它只是没有检测到主datepicker函数内部的第二个datepicker并给我错误-SCRIPT438:对象不支持属性或方法“ datepicker”

So I read the following post and decided to do stuff the following way. 因此,我阅读了以下帖子,并决定按照以下方式进行操作。 This works but obviously I dont have the range functionality now. 这有效,但显然我现在没有范围功能。

$(document).ready(function(){

$('#from').datepicker({
            changeMonth: true,
            changeYear: true,
            numberOfMonths: 1,
            showButtonPanel: true,
    onSelect: function(dateText, inst) { 
    $('[name=wmFrom]').val(dateText); //need this part for the search filters
    }

});
 $('#to').datepicker({
            changeMonth: true,
            changeYear: true,
            numberOfMonths: 1,
            showButtonPanel: true,
    onSelect: function(dateText, inst){ 
    $('[name=wmTo]').val(dateText); //need this part for the search filters
    }       
});
});

Ok now, I am still learning js and jquery. 好的,现在我仍在学习js和jquery。 Can any one help me implement a range functionality for this without calling/initializing datepicker inside datepicker. 谁能帮我实现范围功能,而无需在datepicker中调用/初始化datepicker。 I know this problem probably means there is probably some issue with versions or compatibility of the libraries. 我知道这个问题可能意味着库的版本或兼容性可能存在问题。 But like I said I am really cant get rid of them or change much in this. 但是就像我说的那样,我真的不能摆脱它们或对此做出很大的改变。 Any idea/help will be very much appreciated.. 任何想法/帮助将不胜感激。


MORE INFO- 更多信息-

This is frustrating because someone who worked before me have done things this way and any change I make break different functions. 令人沮丧的是,在我之前工作的某人以这种方式做事,而我所做的任何更改都破坏了功能。 This is an internal application so cant give a link :(. 这是一个内部应用程序,因此无法提供链接:(。

This is what I am talking about - First there is a header php which is included in the top of the problem file I am working on. 这就是我在说的-首先,有一个标头php包含在我正在处理的问题文件的顶部。 This header has the link to jquery library. 该头具有指向jquery库的链接。 After including the header.php there are links to queryLoader.js, jquery.easing.1.3.js. 包含header.php之后,有指向queryLoader.js,jquery.easing.1.3.js的链接。 Then There is a bunch of code which has scripts and the body html (this is where the html inputs associated with the datepicker are). 然后是一堆包含脚本和正文html的代码(这是与datepicker关联的html输入所在的位置)。 Then they link /full/jquery.tools.min.js , jquery.effects.core.js, jquery.ui.core.js, jquery.ui.widget.js, jquery.ui.datepicker, query.ui.tooltip.js , jquery-ui.css. 然后,它们链接/full/jquery.tools.min.js,jquery.effects.core.js,jquery.ui.core.js,jquery.ui.widget.js,jquery.ui.datepicker,query.ui.tooltip。 js,jquery-ui.css。 After this, there are more scripts which use functions like $.tools.validator.fn and serializeObject(). 此后,还有更多脚本使用$ .tools.validator.fn和serializeObject()之类的功能。

If I assume its loading jquery lib twice and change the /full/jquery.tools.min.js to /tiny/jquery.tools.min.js or /all/jquery.tools.min.js I get the following error: Object doesn't support this property or method on lines with functions such as serializeObject(). 如果我假设两次加载jquery lib,并将/full/jquery.tools.min.js更改为/tiny/jquery.tools.min.js或/all/jquery.tools.min.js,则会收到以下错误:对象在具有诸如serializeObject()之类的函数的行上不支持此属性或方法。 I get this when the page loads itself. 当页面加载时,我得到了这个。

If I add the /jquery.tools.min.js after all the UI class links, I get: Object doesn't support property or method 'datepicker' at the point of datepicker initialization on the input. 如果在所有UI类链接之后添加/jquery.tools.min.js,则会得到:对象在输入的日期选择器初始化时不支持属性或方法“日期选择器”。 I get this when the page loads itself. 当页面加载时,我得到了这个。

Who should I do :( 我该怎么办:(

I try to help you after 4+ years :) 我会在4年后为您提供帮助:)

$(document).ready(function(){
  //datapickerer creation... $('#to').datepicker({...

  $("#to").on( "change", function() { 
            $("#from").datepicker("option", "maxDate", getDate( this ));
  });
  $("#from").on( "change", function() { 
            $("#to").datepicker("option", "minDate", getDate( this ));
  });
});

A common headache with jQuery tools is they have versions that include full jQuery.js in their plugin file. jQuery工具的一个常见问题是它们的版本在其插件文件中包含完整的jQuery.js。 This can cause problems if jQuery tools gets loaded after other plugins. 如果在其他插件之后加载jQuery工具,则可能导致问题。 It is a terrible practice for a plugin to include full jQuery library in their file... but the jQuery tools team thinks it's great. 插件在其文件中包含完整的jQuery库是一种可怕的做法。但是jQuery工具团队认为这很棒。 Check the version you are using as a start. 检查您用作开始的版本。

Essentially the problem can boil down to user not realizing jQuery is being loaded twice. 本质上,问题可能归结为用户没有意识到jQuery被两次加载。 Loading jQuery on top of another versio that already has plugins registered will wipe out those plugins. 在另一个已经注册了插件的版本上加载jQuery将清除这些插件。

Use a browser console to check for errors thrown to help you troubleshoot 使用浏览器控制台检查抛出的错误以帮助您进行故障排除

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

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