无法识别JQUERY datetimepicker onSelect

时间:2018-04-30 11:47:58

标签: javascript jquery datepicker datetimepicker onselect

我正在尝试使用jquery中的datetimepicker。我想在您选择新日期时清除一些输入字段,但我的onSelect不起作用。 首先,它只在文档加载时触发一次。之后它再也不会发射了。 其次,onSelect不能与匿名函数一起使用。

这不起作用

$(document).ready(function() {
  $(function () {
    $('#dateValue').datetimepicker({
      format: 'YYYY-MM-DD',
      onSelect: function() {
        console.log("testing");
      }
    });
  });
});

这确实有效,但仅在文档加载时触发,然后再从不

$(document).ready(function() {
  $(function () {
    $('#dateValue').datetimepicker({
      format: 'YYYY-MM-DD',
      onSelect: console.log("testing")
    });
  });
});

我在head.html中的导入文件在index.html中

<head>
    <meta charset="UTF-8">
    <title>....</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment.js"></script>
    <script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.37/js/bootstrap-datetimepicker.min.js"></script>
    <script type="text/javascript" src="script.js"></script>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.37/css/bootstrap-datetimepicker.min.css" rel="stylesheet"/>
    <link rel="stylesheet" href="myCSS.css">
</head>

编辑:我傻了,忘了给出错误信息

Uncaught TypeError: option onSelect is not recognized!
    at Function.<anonymous> (bootstrap-datetimepicker.min.js:8)
    at Function.each (jquery.min.js:2)
    at Object.l.options (bootstrap-datetimepicker.min.js:8)
    at c (bootstrap-datetimepicker.min.js:9)
    at HTMLInputElement.<anonymous> (bootstrap-datetimepicker.min.js:9)
    at Function.each (jquery.min.js:2)
    at n.fn.init.each (jquery.min.js:2)
    at n.fn.init.a.fn.datetimepicker (bootstrap-datetimepicker.min.js:9)
    at HTMLDocument.<anonymous> (script.js:3)
    at j (jquery.min.js:2)

3 个答案:

答案 0 :(得分:4)

您必须使用在更改日期时触发的dp.change事件。

$('#dateValue').datetimepicker({
  format: 'YYYY-MM-DD',
});

$("#dateValue").on("dp.change", function() {
  console.log("testing");
});
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment.js"></script>
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.37/js/bootstrap-datetimepicker.min.js"></script>

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.37/css/bootstrap-datetimepicker.min.css" rel="stylesheet" />

<div class='input-group date' id='dateValue'>
  <input type='text' class="form-control" />
  <span class="input-group-addon">
    <span class="glyphicon glyphicon-calendar"></span>
  </span>
</div>

答案 1 :(得分:0)

试试声明

$("#dateValue").on("onselect",function(){ console.log("testing"); });

http://api.jquery.com/on/

答案 2 :(得分:0)

您使用的是Bootstrap datetimepicker组件。onselect事件不是其中的一部分。

你必须使用

$("#dateValue").on("dp.change", function (e) {
//your code
});