จะแสดงวันที่มาจาก jQuery เพื่อแสดงวันที่ปัจจุบันได้อย่างไร

จะแสดงวันที่มาจาก jQuery เพื่อแสดงวันที่ปัจจุบันได้อย่างไร ฉันมีรายการแบบเลื่อนลงและป้อนข้อมูลด้วย datepicker

ตอนนี้สิ่งที่ฉันทำคือ เมื่อผู้ใช้เลือกปี 2019-2020 จากเมนูแบบเลื่อนลง ฉันกำลังแสดงวันที่ในช่องป้อนข้อมูล 31/03/2020

หากเลือก 2020-2021 ก็จะแสดง 31/03/2021 ไปเรื่อยๆ

ฉันหมายถึงวันที่และเดือนได้รับการแก้ไขเพียงเปลี่ยนปีเท่านั้น

I am getting my output in the input field but when I click on the input field then it's showing the current date. I need whatever the date comes from the jquery that date auto selected.

$("#year").change(function() {
  var dropdownDuration = this.value;
  var items = dropdownDuration.split('-');
  //alert("31/03/"+items[1]);
  $("#yearDate").val("31/03/" + items[1]);
});

$("#yearDate").datepicker();
<link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css">
<select class="form-control" name="year" id="year">
  <option value="" disabled selected>Year</option>
  <option value="2019-2020">2019-2020 </option>
  <option value="2020-2021">2020-2021 </option>
  <option value="2021-2022">2021-2022 </option>
</select>

<input type="text" name="enddate" class="form-control datepicker" id="yearDate">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.js"></script>


person user9437856    schedule 06.01.2019    source แหล่งที่มา


คำตอบ (1)


คุณสามารถใช้พารามิเตอร์ datepicker เช่นนี้:

$("#yearDate").datepicker({dateFormat: 'dd/mm/yy',defaultDate:this.value});

ลองตามด้านล่างนี้ครับ

$("#year").change(function() {
  var dropdownDuration = this.value;
  var items = dropdownDuration.split('-');
  //alert("31/03/"+items[1]);
  $("#yearDate").val("31/03/" + items[1]);
});

$("#yearDate").datepicker({
    dateFormat: 'dd/mm/yy',
    defaultDate: this.value
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css">
<select class="form-control" name="year" id="year">
  <option value="" disabled selected>Year</option>
  <option value="2019-2020">2019-2020 </option>
  <option value="2020-2021">2020-2021 </option>
  <option value="2021-2022">2021-2022 </option>
</select>

<input type="text" name="enddate" class="form-control datepicker" id="yearDate">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.js"></script>

person Igor Carvalho    schedule 06.01.2019
comment
ขอบคุณสำหรับคำตอบครับ ขอเวลาตรวจสอบหน่อยนะครับ - person user9437856; 06.01.2019
comment
มันทำงานได้อย่างสมบูรณ์แบบสำหรับฉัน ขอบคุณสำหรับความช่วยเหลือ โหวตจากด้านข้างของฉัน - person user9437856; 06.01.2019
comment
ฉันยินดีที่จะช่วยคุณ เราสามารถรักษาความสัมพันธ์ที่ลึกซึ้งและต่อไปได้ ขอบคุณ - person Igor Carvalho; 06.01.2019
comment
หากคุณมีเวลา โปรดตรวจสอบปัญหานี้ stackoverflow.com/questions/54050168/ - person user9437856; 06.01.2019