我正在尝试使用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")
    });
  });
});

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

最佳答案

您必须使用 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>

10-05 23:36