twitter-bootstrap 为什么我的 Bootstrap 日期选择器不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37547716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 23:57:17  来源:igfitidea点击:

Why is my Bootstrap date-picker not working?

javascriptjquerytwitter-bootstrap

提问by Daniel

I tried to use Bootstrap date-picker (https://github.com/eternicode/bootstrap-datepicker), but i can't get it to work.

我尝试使用 Bootstrap 日期选择器 ( https://github.com/eternicode/bootstrap-datepicker),但我无法让它工作。

On jsfiddle everything works like a charm: http://jsfiddle.net/hwuktpt2/

在 jsfiddle 上,一切都像一个魅力:http: //jsfiddle.net/hwuktpt2/

After some failed tests on the actual page, I even tried a blank new page and integrated everything from scratch again. jQuery works, Bootstrap styling works, Datatables works, but I just can't get date-picker to work.

在实际页面上进行了一些失败的测试后,我什至尝试了一个空白的新页面,并重新从头开始整合所有内容。jQuery 工作,Bootstrap 样式工作,数据表工作,但我就是无法让日期选择器工作。

Any suggestions?

有什么建议?

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/u/bs-3.3.6/jq-2.2.3,jszip-2.5.0,pdfmake-0.1.18,dt-1.10.12,b-1.2.0,b-colvis-1.2.0,b-html5-1.2.0,b-print-1.2.0,fh-3.1.2,se-1.2.0/datatables.min.css"/>
    <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.6.1/css/bootstrap-datepicker.min.css"/>
    <script type="text/javascript" src="https://cdn.datatables.net/u/bs-3.3.6/jq-2.2.3,jszip-2.5.0,pdfmake-0.1.18,dt-1.10.12,b-1.2.0,b-colvis-1.2.0,b-html5-1.2.0,b-print-1.2.0,fh-3.1.2,se-1.2.0/datatables.min.js"></script>  
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.6.1/js/bootstrap-datepicker.min.js"></script> 
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.6.1/locales/bootstrap-datepicker.de.min.js"></script> 
    <script type="text/javascript">
        $('.date_picker input').datepicker({
          format: "dd.mm.yyyy",
          todayBtn: "linked",
          language: "de"
        });
    </script>
    <style type="text/css">
        .control-label {
            padding-top:7px;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="page-header">
            Neues Mobile Device anlegen
        </div>
    </div>
    <div class="container">
      <form class="form-horizontal" name="mobdev_neu" action="mobdev_neu.php" method="post">
        <div class="form-group">
          <label for="mobdev_neu_type" class="col-xs-2 control-label">Type</label>
          <div class="col-xs-10">
            <select id="mobdev_neu_type" class="form-control" name="mobdev_neu_type" REQUIRED>
                <option value="">-- Bitte ausw?hlen --</option>
            </select>
          </div>
        </div>
        <div class="form-group">
          <label for="mobdev_neu_imei" class="col-xs-2 control-label">IMEI</label>
          <div class="col-xs-10">
            <input type="text" id="mobdev_neu_imei" class="form-control" name="mobdev_neu_imei" placeholder="Pflichtfeld" REQUIRED>
          </div>
        </div>
        <div class="form-group">
          <label for="mobdev_neu_kaufdatum" class="col-xs-2 control-label">Kaufdatum</label>
          <div class="col-xs-10 date_picker">
            <input type="text" id="mobdev_neu_kaufdatum" class="form-control" name="mobdev_neu_kaufdatum" placeholder="Pflichtfeld">
          </div>
        </div>
      </form>
    </div>
</body>

回答by William Gates

Wrap the date picker initialize call in a document.ready. This code is running before the dom is finished loading from the look of it. Since JavaScript is interpreted it is run at the time it is read by the browser. Because you're defining the code in the head the body and its contents haven't been loaded yet; so the selector finds no elements to initialize datepicker. If you don't want to use document.ready functionality you could also move the script to the end of the body tag.

将日期选择器初始化调用包装在 document.ready 中。这段代码在 dom 完成加载之前运行。由于 JavaScript 被解释,它在浏览器读取时运行。因为您在头部定义代码,所以主体及其内容尚未加载;所以选择器找不到要初始化日期选择器的元素。如果您不想使用 document.ready 功能,您还可以将脚本移动到 body 标签的末尾。

    $(function(){
        $('.date_picker input').datepicker({
           format: "dd.mm.yyyy",
           todayBtn: "linked",
           language: "de"
        });
    });

回答by user6444905

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.37/css/bootstrap-datetimepicker.min.css" />
<script src="http://cdnjs.cloudflare.com/ajax/libs/moment.js/2.5.1/moment.min.js"></script>            
<script src="http://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/3.0.0/js/bootstrap-datetimepicker.min.js"></script>

Use these Libraries after using this use below code in you body where you want to make Date field.

在您想要制作日期字段的身体中使用以下代码后,使用这些库。

<div class='input-group date' id='datetimepicker1'>
<input type='text' class="form-control" name="update_time" placeholder="Update Date"/>
                        <span class="input-group-addon">
                            <span class="glyphicon glyphicon-calendar"></span>
                        </span>
                    </div>
                    <script type="text/javascript">
                        var j = jQuery.noConflict();
                        j(function () {
                            j('#datetimepicker1').datetimepicker({
                                format: 'L',
                                disabledHours: true,
                            });
                        });
                    </script>