简体   繁体   中英

jQuery Datepicker won't change value of input

I use the datepicker function from jquery. 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:

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

$('.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 .

<input id="datepicker" />

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

You also don't need to change the input text with the onSelect event. The widget will do thix automatically.

$('#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. Check this link for more examples.

Here is the code from 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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