簡體   English   中英

如何在第一個日期選擇器選擇DATE時在第二個日期選擇器中設置DATE?

[英]How to set DATE in second date-picker on selection of DATE from first date-picker?

我正在嘗試從第一個date-picker選擇date-picker中選擇日期時設置日期date-picker date-picker ,並且如果用戶選擇了另一個日期,則希望顯示警告消息。 我無法做到這一點。

這是我的代碼:

$('#ELEdatepicker1').datepicker({
      autoclose: true,
      endDate: new Date(),
      todayHighlight: true,
    }).change(function(){
      getELEdifference($(this),$('#ELEdatepicker2'));
    });
    $('#ELEdatepicker2').datepicker({
      autoclose: true,
      startDate: new Date(),
      todayHighlight: true,
    }).change(function(){
      getELEdifference($('#ELEdatepicker1'),$(this));
      alert('Are you sure ?');
    });

  function getELEdifference($this1,$this2)
  {
    if($this1.datepicker("getDate") != null && $this2.datepicker("getDate") != null)
    {
      var ELEcertDiff= $this1.datepicker("getDate") - $this2.datepicker("getDate");    
      var result = ELEcertDiff / (1000 * 60 * 60 * 24) * -1;
      //$("#ELEcertDiff").text(result);
      document.getElementById("ELEcertDiff").value = result;
    }
  }

如何在第二個日期選擇器中顯示日期(假設一年后)? 如果用戶在一年之前或之后選擇,它將在日期選擇上顯示警告消息。

更新:是否可以傳遞參數來設置第二個日期選擇器的差異? 對於Ex。 在此輸入圖像描述 在上圖中如果我從發行日期選擇器和持續時間下拉列表框中選擇日期,請選擇一個值(例3),然后在到期日期 - 選擇器顯示從所選日期起3年后的日期。

我提到了一些問題:

Jquery UI:日期選擇器。 如何通過$ _GET在日期選擇器中設置日期

從第一個日期選擇器定義第二個日期選擇器的開始日期

我是JavaScript和JQuery的新手。 歡迎任何形式的幫助。 提前致謝。

plz添加並檢查此代碼:

$('#ELEdatepicker1').datepicker({
    autoclose: true,
    todayHighlight: true,
    dateFormat:'dd-mm-yy',
    onClose: function( selectedDate ) {
        var d = new Date(selectedDate);
        var day = d.getDate();
         var month = d.getMonth();
         var year = d.getFullYear() + 1;
        var newdate = month + "-" + day + "-" + year;
        $( "#ELEdatepicker2" ).datepicker( "option", "minDate", newdate );
         $('.highlight a', $(this).next()).addClass('ui-state-highlight');
    }
});

$('#ELEdatepicker2').datepicker({
    autoclose: true,
    todayHighlight: true,
    dateFormat:'dd-mm-yy',
    onClose: function( selectedDate ) {
        $("#ELEdatepicker2" ).datepicker( "option", "maxDate", selectedDate );
        $('.highlight a', $(this).next()).addClass('ui-state-highlight');
    }
});

檢查這個答案

<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<input type="text" id="ELEdatepicker1">
<input type="text" id="ELEdatepicker2">
<!-- <input type="text" id="ELEcertDiff"> -->
<script>
$('#ELEdatepicker1').datepicker({
    autoclose: true,
    todayHighlight: true,
    dateFormat:'dd-mm-yy',
    onClose: function( selectedDate ) {
        var d = new Date(selectedDate);
        var year = d.getFullYear() + 1;
        var month = d.getMonth();
        var day = d.getDate();
        var newdate = year + "-" + month + "-" + day;
        $( "#ELEdatepicker2" ).datepicker( "option", "minDate", newdate );
         $('.highlight a', $(this).next()).addClass('ui-state-highlight');
    }
});

$('#ELEdatepicker2').datepicker({
    autoclose: true,
    todayHighlight: true,
    dateFormat:'dd-mm-yy',
    onClose: function( selectedDate ) {
        $("#ELEdatepicker2" ).datepicker( "option", "maxDate", selectedDate );
        $('.highlight a', $(this).next()).addClass('ui-state-highlight');
    }
});
</script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM