无法使用jquery在datepicker中选择更多日期

时间:2016-12-07 02:13:23

标签: jquery datepicker jquery-ui-datepicker

我是新手,我已经设定了开始日期+60 D,我很困惑为什么我不能再选择日期,我的计划是设置默认日期范围+ 60D而我&# 39; d能够选择更多日期。示例链接http://jsfiddle.net/jquerybyexample/LGw6F/

From: <input type="text" id="txtFromDate" />
To: <input type="text" id="txtToDate" />


$(document).ready(function(){
    $("#txtFromDate").datepicker({
        minDate: 0,
        maxDate: "+60D",
        numberOfMonths: 2,
        onSelect: function(selected) {
          $("#txtToDate").datepicker("option","minDate", selected)
        }
    });
    $("#txtToDate").datepicker({ 
        minDate: 0,
        maxDate:"+60D",
        numberOfMonths: 2,
        onSelect: function(selected) {
           $("#txtFromDate").datepicker("option","maxDate", selected)
        }
    });  
});

2 个答案:

答案 0 :(得分:2)

根据最新评论再次更新。

更新了jFiddle(http://jsfiddle.net/LGw6F/2230/

$(document).ready(function(){
   $("#txtFromDate").datepicker({ 
    dateFormat: 'yy-mm-dd',
    changeMonth: true,
    minDate: new Date(),
    numberOfMonths: 2,
    onSelect: function(selected) {
          $("#txtToDate").datepicker("option","minDate",selected);
          var  selectedDate = $("#txtFromDate").datepicker("getDate");
          var msecsInADay = 5184000000;
          var endDate = new Date(selectedDate.getTime() + msecsInADay);
          $("#txtToDate").datepicker("option","maxDate",endDate);

        }
  });

  $("#txtToDate").datepicker({ 
      dateFormat: 'yy-mm-dd',
      numberOfMonths: 2,
  });
});

答案 1 :(得分:2)

要预先填充输入字段(&#34;设置默认日期&#34;),请执行以下操作:

&#13;
&#13;
$(function(){
    $("#txtFromDate").datepicker({
        minDate:0,
        maxDate: "+60D",
        numberOfMonths: 2,
    });
    $("#txtToDate").datepicker({ 
        minDate:0,
        maxDate:"+60D",
        numberOfMonths: 2,
    });
    var default_date = new Date();
    default_date.setDate(default_date.getDate()+60);
    $('#txtFromDate').datepicker('setDate', new Date());
    $('#txtToDate').datepicker('setDate', default_date);
});
$("#txtFromDate").datepicker({
        onSelect: function(dateText) {
            $("#txtToDate").datepicker("option", "minDate", $('#txtFromDate').datepicker("getDate") );
            var date2 = $("#txtFromDate").datepicker("getDate");
            date2.setDate(date2.getDate()+60);
            $("#txtToDate").datepicker("option", "maxDate", date2);
        }
    });
&#13;
body
{
    font-size:8pt;
    font-family:Verdana;
    padding: 5px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<br/>
From: <input type="text" id="txtFromDate" />
To: <input type="text" id="txtToDate" />

<!-- begin snippet: js hide: false console: true babel: false -->
&#13;
&#13;
&#13;

完整的HTML示例:

&#13;
&#13;
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test</title>
<style>
body
{
    font-size:8pt;
    font-family:Verdana;
    padding: 5px;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
</head>
<body>
    
<br/>
From: <input type="text" id="txtFromDate" />
To: <input type="text" id="txtToDate" />

<script type="text/javascript">
$(function(){
	$("#txtFromDate").datepicker({
		minDate:0,
		maxDate: "+60D",
		numberOfMonths: 2,
	});
	$("#txtToDate").datepicker({ 
		minDate:0,
		maxDate:"+60D",
		numberOfMonths: 2,
	}); 
	var default_date = new Date();
	default_date.setDate(default_date.getDate()+60);
	$('#txtFromDate').datepicker('setDate', new Date());
	$('#txtToDate').datepicker('setDate', default_date);
});
$("#txtFromDate").datepicker({
    onSelect: function(dateText) {
        $("#txtToDate").datepicker("option", "minDate", $('#txtFromDate').datepicker("getDate") );
        var date2 = $("#txtFromDate").datepicker("getDate");
        date2.setDate(date2.getDate()+60);
        $("#txtToDate").datepicker("option", "maxDate", date2);
    }
});
</script>

</body>
</html>
&#13;
&#13;
&#13;

我希望这就是你想要的。 此致