【问题标题】:Where is the problem in the datepicker?日期选择器的问题在哪里?
【发布时间】:2010-06-10 09:31:52
【问题描述】:

我有 2 个日期数组。我不明白为什么第一个元素有效,而第二个无效。那是相同的功能。第二和第三(4th 5th ...等)应该可以工作。我不明白。可能是 datepicker 的 bug,因为我也不能使用 onChange 函数。

[2010,8,10] - [2010,8,15] -> 作品 [2010,7,10] - [2010,7,10] -> 不起作用。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html lang="en">
<head>
    <title>jQuery UI Datepicker - Default functionality</title>

    <link type="text/css" href="js/themes/base/ui.all.css" rel="stylesheet" />
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript" src="js/ui/ui.core.js"></script>
    <script type="text/javascript" src="js/ui/ui.datepicker.js"></script>

<style>
  article, aside, figure, footer, header, hgroup, 
  menu, nav, section { display: block; }
  td.odd, table.ui-datepicker-calendar tbody td.odd a { background: yellow; }
  td.odd2, table.ui-datepicker-calendar tbody td.odd2 a { background: red; bgcolor: red; }
</style>

    <script type="text/javascript">


var start_date = [
 [2010,8,10], [2010,7,10] 
 ];

var end_date = [
 [2010,8,15], [2010,7,15] 
 ]; 

function nationalDays(date) {

var year = 0;
var month = 1;
var day = 2

 for (i = 0; i < start_date.length; i++) {

    if (

        ( ( start_date[i][year] <= date.getFullYear() ) && ( date.getFullYear() <= end_date[i][year] ) ) && 

        ( ( start_date[i][month]-1 <= date.getMonth() ) && ( date.getMonth() <= end_date[i][month]-1 ) ) && 

        ( ( start_date[i][day] <= date.getDate() ) && ( date.getDate() <= end_date[i][day] ) ) 

    ) {  
    //( start_year <= now_year <= end_year )  && ( start_month <= now_month <= end_month )  && ( start_day <= now_day <= end_day )

            return [true, 'odd2'];

    } else { 

            return [false, 'odd2'];

    }           
 }
}

    $(function() {
        $(".datepicker").datepicker({
            beforeShowDay: nationalDays,
            showOn: 'button', buttonImage: 'images/calendar_icon.jpg', buttonImageOnly: true,
            numberOfMonths: 3,
            dateFormat: 'dd/mm/yy',
            showButtonPanel: false,
            closeText: 'X' ,
            currentText: 'Now',
            constrainInput: true,
            stepMonths: 3,
            firstDay: 1,
            monthNames: ['Januar','Februar','Marts','April','Maj','Juni','Juli','August','September','Oktober','November','December'],
            nextText: 'Later',
            prevText: 'Earlier',
            minDate: '-0d',
            maxDate: '+1y'
        });
    });

    </script>
</head>
<body>

<table>
<tr>

    <td><p>Date: <input type="text" name="date2" value="" size="20" readonly="readonly" class="datepicker"></p></td>

</tr>
</table>


</body>
</html>

【问题讨论】:

    标签: javascript jquery datepicker


    【解决方案1】:

    我很难解析 if 块,但我认为这并不重要。我猜这是因为你在false 与你的第一组日期不匹配时返回,所以你的循环永远不会超过第一次迭代。

    我认为您想将 return false 移到循环之外...

    【讨论】:

    • 谢谢。我已经解决了这个问题。
    • 你对 datepicker 中的 onChange 函数有什么想法吗?因为这个函数有一个bug。
    • @question_about_the_problem onChange 函数是什么?
    • readonly="readonly" class="datepicker" onChange="dates_controls(-1,-1,-1,'triple');">
    • @lc:适用于手动输入 (onChange),但不适用于 datepicker。
    【解决方案2】:

    在您的 nationalDays 函数中,您正在启动一个循环,但只处理 start_date 数组中的第一个条目,因为无论条件是真还是假,您都会立即从函数返回(返回[true, 'odd2'] 数组或 [false, 'odd2'] 数组,但无论哪种情况,在处理下一个条目之前终止函数。

    【讨论】:

      【解决方案3】:

      谢谢各位。我已经修好了。再次感谢您的帮助。

      function nationalDays(date) {
      
      var year = 0;
      var month = 1;
      var day = 2
      
       for (i = 0; i < start_date.length; i++) {
      
          if (
      
              ( ( start_date[i][year] <= date.getFullYear() ) && ( date.getFullYear() <= end_date[i][year] ) ) && 
      
              ( ( start_date[i][month]-1 <= date.getMonth() ) && ( date.getMonth() <= end_date[i][month]-1 ) ) && 
      
              ( ( start_date[i][day] <= date.getDate() ) && ( date.getDate() <= end_date[i][day] ) ) 
      
          ) {  
          //( start_year <= now_year <= end_year )  && ( start_month <= now_month <= end_month )  && ( start_day <= now_day <= end_day )
      
                  return [true, 'odd2'];
      
          } //else { 
      
                  //return [false, 'odd2'];
          //}         
       }
                  return [false, 'odd2'];
      
      }
      

      【讨论】:

      • 你不必发布这个......只要接受回答你问题的人...... :) 干杯!
      • @Reigel:我只是想帮助别人。如果有人来此页面寻求解决方案,他/她将看到此更改(:
      猜你喜欢
      • 2011-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多