簡體   English   中英

jQuery Datepicker不會改變輸入值

[英]jQuery Datepicker won't change value of input

我使用jquery中的datepicker函數。 現在我已經集成了一個內聯日歷。 應該可以在顯示的月份中點擊一天,然后提交表單或更改網址。 我需要將新日期添加到網址,以便我可以跳轉到此日期。

但我的代碼不會改變輸入的值 - 我不知道為什么。

這是我的代碼:

在頭部區域:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript" src="templates/js/bootstrap-datepicker.js"></script>
<script type="text/javascript" src="templates/js/bootstrap-datepicker.de.js"></script>

HTML:

<form method="get" action="index.php">
  <div class="form-group">
    <div class="minicalendar"></div>
      <input class="form-control" type="text" name="submitDate" id="submitDate" />
  </div>
</form>

JS:

$('.minicalendar').datepicker({
    format: "dd.mm.yyyy",
    todayBtn: true,
    language: "de",
    calendarWeeks: true,
    todayHighlight: true,
    onSelect: function(dateText, inst) { 
      $('#submitDate').val(dateText);
   }
});

你知道為什么嗎? 我現在正在尋找一段時間,但我無法找到有效的解決方案......

謝謝。

您將datepicker綁定到div而不是input

<input id="datepicker" />

$('#datepicker').datepicker({
     //...
});

您也無需使用onSelect事件更改輸入文本。 小部件將自動執行thix。

$('#submitDate').datepicker({
    format: "dd.mm.yyyy",
    todayBtn: true,
    language: "de",
    calendarWeeks: true,
    todayHighlight: true,
    onSelect: function(dateText, inst) { 
      $('#submitDate').val(dateText);
   }
});

用這個。 Datepicker不會綁定到<div>它必須是<input>元素。 有關更多示例,請查看此鏈接

以下是JQuery UI DatePicker的代碼:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>jQuery UI Datepicker - Default functionality</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  <link rel="stylesheet" href="/resources/demos/style.css">
  <script>
  $(function() {
    $( "#datepicker" ).datepicker();
  });
  </script>
</head>
<body>

<p>Date: <input type="text" id="datepicker"></p>


</body>
</html>

輸入id必須與在JQuery中選擇的元素相同。

暫無
暫無

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

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