简体   繁体   English

Boostrap日期选择器不会在每个更改事件上更新

[英]Boostrap date picker is not updating on each change event

I am trying to change the to date on select from date . 我试图将select date更改为date。 But it's not updating on each change , working only first time. 但这并不是每次更改都会更新,只能在第一次使用。

This is my code 这是我的代码

<div class="control-group">
<label class="control-label wth-auto" for="txt_from_date">From<span class="star">*</span></label>
<div class="controls">
    <div class="input-append date dp1" data-date-format="yyyy-mm-dd" data-date="">
        <input class="span5 lms_datepicker" data-date-format="yyyy-mm-dd" name="txt_from_date"  readonly="" value="<?php echo date('d/m/Y') ?>" placeholder="<?php echo date('d/m/Y') ?>" size="26" type="text" id="txt_from_date">
        <span class="add-on">
            <i class="icon-calendar"></i>
        </span>
    </div>
    <div class="input-append date lms_datepicker" data-date-format="yyyy-mm-dd" data-date="">
        <input class="span5 lms_datepicker" data-date-format="yyyy-mm-dd" name="txt_to_date"  readonly="" value="<?php echo date('d/m/Y') ?>" placeholder="<?php echo date('d/m/Y') ?>" size="26" type="text" id="txt_to_date">
        <span class="add-on">
            <i class="icon-calendar"></i>
        </span>
    </div>

</div>

$('.lms_datepicker').datepicker({});
$("#txt_from_date").on("changeDate", function(e) {
 var nowDate = new Date($(this).val());
 var now = new Date(nowDate.getFullYear(), nowDate.getMonth(), nowDate.getDate(), 0, 0, 0, 0);
 $('#txt_to_date').datepicker('remove');
 $("#txt_to_date").datepicker({
        onRender:  function(date) {
            return date.valueOf() < now.valueOf() ? 'disabled old' : '';
        } });
  });

Please suggest where it is wrong. 请指出哪里有问题。

Thanks in advance. 提前致谢。

You cannot create datepicker by class this way, only a single datepicker object will be created, you need to iterate through the classes and create a datepicker for each DOM object, then use the datepicker setDate function to set the date for the needed datepicker by selecting the DOM object: 您不能以这种方式按类创建datepicker,只能创建一个datepicker对象,您需要遍历这些类并为每个DOM对象创建一个datepicker,然后使用datepicker setDate函数通过选择所需的datepicker设置日期。 DOM对象:

          $('.lms_datepicker').each(function(){
                $(this).datepicker();
            });

            $("#txt_from_date").on("change", function(e) {

             var nowDate = new Date($(this).val());
             var now = new Date(nowDate.getFullYear(), nowDate.getMonth(), nowDate.getDate(), 0, 0, 0, 0);
             $('#txt_to_date').datepicker("setDate",now);

       // <<<<<<<<<<<<<<<<<<<<<<<<<<<<< let it start form the date chosen in 
       //from all previous dates will be disabled 
       $('#txt_to_date').datepicker( "option", "minDate", nowDate);

        // <<<<<<<<<<<<<<<<<<<<<< Edit - disable the from datepicker
        $(this).datepicker( "option", "disabled", true );

            });

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

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