Bootstrap Datepicker无法更改endDates

时间:2017-06-20 02:19:03

标签: jquery bootstrap-datepicker

我收到了名为“类型”的单选按钮,其值为“标准”和“试用”。然后我还有一个文本框,其中id为“ datepicker ”。 因此,当我点击标准按钮时,datepicker的endDate假设 3天,当我点击试用按钮时,datepicker的endDate假设 1天。< / p>

我现在的问题是,当我选中“标准”值的单选按钮时,endDate将变为 3天。但是,当我选中带有“试用”值的单选按钮时,endDate仍然 3天。否则当我首先检查“试用”时,该值将变为 1天,当选中“标准”时,endDate的结果将相同那是 1天

我尝试了$(".datepicker").datepicker("refresh");$(".datepicker").datepicker("destroy");,但似乎无效。

    <script type="text/javascript" src="~/Scripts/jquery-2.0.2.js"></script>
    <link rel="stylesheet" type="text/css" href="~/Content/datepicker.min.css">
    <script type="text/javascript" src="~/Scripts/bootstrap-datepicker.js"></script>
    <script type="text/javascript" src="~/Scripts/bootstrap.min.js"></script>
    <link rel="stylesheet" type="text/css" href="~/Content/bootstrap.min.css">

    <script type="text/javascript">
        $(window).load(function () {
            $('.datepicker').val('dd/mm/yyyy');
            dateExpiry();
        });
        $("input[name='Type']").change(function () {
            $('.datepicker').val('dd/mm/yyyy');
            dateExpiry();
        });

        function dateExpiry() {
            var type = $("input[name='Type']:checked").val();
            if (type == 'Trial') {
                $('.datepicker').datepicker({
                    format: 'dd/mm/yyyy',
                    startDate: '-0d',
                    endDate: '+1d',
                    autoclose: true
                });
            }
            else if (type == 'Standard') {
                $('.datepicker').datepicker({
                    format: 'dd/mm/yyyy',
                    startDate: '-0d',
                    endDate: '+3d',
                    autoclose: true
                });
            }
        };
    </script>

1 个答案:

答案 0 :(得分:1)

不要创建一个新的datepicker。使用setEndDate方法设置结束日期值。

&#13;
&#13;
$('.datepicker').datepicker({
  format: 'dd/mm/yyyy',
  startDate: '-0d',
  endDate: '+1d',
  autoclose: true
});
ChangeRange();
$("input[name='Type']").change(ChangeRange);
function ChangeRange() {
  if ($("input[name='Type']:checked").val()=="Standard") {
    $('.datepicker').datepicker('setEndDate', '+3d');
  }
  else if ($("input[name='Type']:checked").val()=="Trial") {
    $('.datepicker').datepicker('setEndDate', '+1d');
  }
}
&#13;
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.7.0/css/bootstrap-datepicker.min.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.7.0/js/bootstrap-datepicker.min.js"></script>

<div class="checkbox">
  <label>
    <input type="radio" name="Type" value="Standard">
    Standard
  </label>
</div>
<div class="checkbox">
  <label>
    <input type="radio" name="Type" value="Trial" checked="checked">
    Trial
  </label>
</div>
<div class="input-group">
  <input type="text" class="form-control datepicker" value="21/06/2017">
  <div class="input-group-addon">
    <span class="glyphicon glyphicon-th"></span>
  </div>
</div>
&#13;
&#13;
&#13;