将jQuery UI Timepicker Addon与React一起使用

Using the jQuery UI Timepicker Addon with React

本文关键字:React 一起 Addon jQuery UI Timepicker      更新时间:2023-09-26

我正试图在我正在编写的React组件中使用jQuery UI Timepicker Addon。然而,每当我尝试使用它时,我都会遇到一个错误,即Addon添加到jQuery的"datetimepicker"函数没有定义。

我认为我遇到的部分(或全部)问题是React中没有用于此的模块,但我正在通过React导入jQuery。我想做的…

index.html:

<!DOCTYPE html>
<html lang='eng'>
  <head>
    <meta charset='utf-8'>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- jQuery UI core CSS -->
    <link rel="stylesheet" href="dist/jquery-ui.css">
    <!-- jQuery UI Timepicker Addon CSS -->
    <link rel="stylesheet" href="dist/jquery-ui-timepicker-addon.css">
</head>
  <body>
    <p style="color: white">
      If you see this there is a problem with React on the site.
    </p>
    <script src="dist/bundle.min.js"></script>
    <script src="js/jquery-ui.min.js"></script>
    <script src="js/jquery-ui-timepicker-addon.js"></script>
  </body>
</html>

正如您所看到的,由于Addon目前还不是React组件(至少,如果它存在的话,我找不到它),所以我手动将它与jQuery UI一起导入。

我的组件(通过其他组件包含在索引中):

/** @jsx React.DOM */
var React = require('react');
var jQ    = require('jquery');
var Time = React.createClass({
    componentDidMount: function() {
        jQ('#startDate').datetimepicker({
            timeFormat: "HH:mm:ss",
            onClose: function() {
                console.log("Timepicker closed");
            }
        });
    },
    render: function() {
        return (
            <input id="startDate" className="input-xs datetime modern" type="datetime-local" />
        );
    }
});
module.exports = {Time: Time};

我应该如何将Timepicker Addon包含在这个React组件中?

附带说明:我之所以使用Timepicker,很大程度上是因为它在其他元素上的弹出效果,以及它对秒和各种时间格式的支持。我现在使用的是"datetime local"输入,但它们并没有真正减少输入:(

尝试在此处添加jQuery:

<script src="js/jquery.min.js"></script>
<script src="js/jquery-ui.min.js"></script>
<script src="js/jquery-ui-timepicker-addon.js"></script>
<script src="dist/bundle.min.js"></script>

删除

var jQ    = require('jquery');

并使用$而不是jQ

$('#startDate').datetimepicker({
        timeFormat: "HH:mm:ss",
        onClose: function() {
            console.log("Timepicker closed");
        }
    });

我还认为您可以使用这个.getDOMNode()而不是"#startDate"

 $(this.getDOMNode()).datetimepicker({
        timeFormat: "HH:mm:ss",
        onClose: function() {
            console.log("Timepicker closed");
        }
    });