简体   繁体   English

jQuery Datepicker不会改变输入值

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

I use the datepicker function from jquery. 我使用jquery中的datepicker函数。 Now I have integrated a inline calendar. 现在我已经集成了一个内联日历。 It should be possible to click on one day in the shown month and then submit a form or change the url. 应该可以在显示的月份中点击一天,然后提交表单或更改网址。 I need the new date added to the url so i can jump to this date. 我需要将新日期添加到网址,以便我可以跳转到此日期。

But my code won't change the value of the input - and i don't know why. 但我的代码不会改变输入的值 - 我不知道为什么。

Here my code: 这是我的代码:

In the head area: 在头部区域:

<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>

The HTML: 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>

The JS: JS:

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

Do you know why? 你知道为什么吗? I was searching for a time now but I wasn't able to find a solution that works ... 我现在正在寻找一段时间,但我无法找到有效的解决方案......

Thanks. 谢谢。

You are binding the datepicker to a div instead of an input . 您将datepicker绑定到div而不是input

<input id="datepicker" />

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

You also don't need to change the input text with the onSelect event. 您也无需使用onSelect事件更改输入文本。 The widget will do thix automatically. 小部件将自动执行thix。

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

Use this. 用这个。 Datepicker won't bind to a <div> it has to be an <input> element. Datepicker不会绑定到<div>它必须是<input>元素。 Check this link for more examples. 有关更多示例,请查看此链接

Here is the code from JQuery UI DatePicker : 以下是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>

The input id has to be the same that the element selected in JQuery. 输入id必须与在JQuery中选择的元素相同。

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

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