时间选择器问题

时间:2020-01-02 05:30:16

标签: jquery html css timepicker

我在项目中使用了时间选择器,但无法正常工作,这给了我未捕获的类型错误。

当我单击文本框时,我需要这样的输出

我尝试以这种方式使用时间选择器:

<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.11.0/jquery-ui.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/clockpicker/0.0.7/bootstrap-clockpicker.min.css">
<title>TimePicker</title>
</head>
<body>
<div class="container">
  <h2>Time Picker</h2>
  <p>This example of time picker using bootstrap and jquery.</p>
  <form>
    <div class="form-group">
      <label for="starttime">Start Time:</label>
      <input type="text" class="form-control" id="starttime" readonly>
    </div>
    <div class="form-group">
      <label for="endtime">End Time:</label>
      <input type="text" class="form-control" id="endtitme" readonly>
    </div>
  </form>
</div>
<script>
$(".starttime").clockpicker({
  twelvehour: true,
});
$("#endtitme").clockpicker({
  twelvehour: true,
});
</script>
</body>
</html>

提前谢谢

3 个答案:

答案 0 :(得分:1)

您应该添加Clockpicker库,但是您仅添加了样式表。在http://weareoutman.github.io/clockpicker/dist/jquery-clockpicker.min.js行之前添加...bootstrap-clockpicker.min.css

答案 1 :(得分:1)

您的代码中缺少clock js

添加clock js及其工作,并将.starttime更改为#starttime,因为它在您的字段中的ID不是class

<script src="http://weareoutman.github.io/clockpicker/dist/jquery-clockpicker.min.js"></script>

$("#starttime").clockpicker({
  twelvehour: true,
  autoclose: true
});
$("#endtitme").clockpicker({
  twelvehour: true,
  autoclose: true 
});
<html>

<head>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
  <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  <script src="http://code.jquery.com/ui/1.11.0/jquery-ui.js"></script>
  <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/clockpicker/0.0.7/bootstrap-clockpicker.min.css">
  <script src="http://weareoutman.github.io/clockpicker/dist/jquery-clockpicker.min.js"></script>
  <title>TimePicker</title>
</head>

<body>
  <div class="container">
    <h2>Time Picker</h2>
    <p>This example of time picker using bootstrap and jquery.</p>
    <form>
      <div class="form-group">
        <label for="starttime">Start Time:</label>
        <input type="text" class="form-control" id="starttime" readonly>
      </div>
      <div class="form-group">
        <label for="endtime">End Time:</label>
        <input type="text" class="form-control" id="endtitme" readonly>
      </div>
    </form>
  </div>

</body>

</html>

在您的时钟选择器中添加autoclose: true

答案 2 :(得分:1)

在document.ready中试用。

您也没有元素具有“ starttime”类 将.starrttime更改为#starttime

    <title>TimePicker</title>
    </head>
    <body>
    <div class="container">
      <h2>Time Picker</h2>
      <p>This example of time picker using bootstrap and jquery.</p>
      <form>
        <div class="form-group">
          <label for="starttime">Start Time:</label>
          <input type="text" class="form-control" id="starttime" readonly>
        </div>
        <div class="form-group">
          <label for="endtime">End Time:</label>
          <input type="text" class="form-control" id="endtitme" readonly>
        </div>
      </form>
    </div>
    <script>
    $(function() {
        $("#starttime").clockpicker({
          twelvehour: true,
        });

        $("#endtitme").clockpicker({
          twelvehour: true,
       });  
    });

    </script>
    </body>
    </html>

如果仍然不起作用 将“ clockpicker”类放入div标签。

        <div class="form-group clockpicker">
          <label for="starttime">Start Time:</label>
          <input type="text" class="form-control" id="starttime" readonly>
        </div>
        <div class="form-group clockpicker">
          <label for="endtime">End Time:</label>
          <input type="text" class="form-control" id="endtitme" readonly>
        </div>