如何禁用Bootstrap datetimepicker

时间:2017-05-18 20:14:21

标签: javascript jquery twitter-bootstrap bootstrap-datetimepicker eonasdan-datetimepicker

我正在使用Bootstrap datatimepicker(https://eonasdan.github.io/bootstrap-datetimepicker/)。我想在页面加载时禁用datetimepicker。

refetch

我在(http://eonasdan.github.io/bootstrap-datetimepicker/Functions/#disable)尝试了$(document).ready(function () { $('#datetimepicker1').datetimepicker({ defaultDate: new Date(), format: 'DD/MM/YYYY hh:mm:ss A' }); }); <div class='input-group date' id='datetimepicker1'> <input type='text' class="form-control" /> <span class="input-group-addon"> <span class="glyphicon glyphicon-calendar"></span> </span> </div> $('#datetimepicker1').prop('disabled', true)。都没有工作。那么最好的方法是什么呢?

3 个答案:

答案 0 :(得分:2)

尝试以下代码$('#datetimepicker1 > .form-control').prop('disabled', true);  请查看链接https://jsfiddle.net/komal10041992/DTcHh/32829/

答案 1 :(得分:2)

你接近解决方案,你必须使用disable(),但你必须记住,正如docs所说:

  

注意所有功能都可通过数据属性访问,例如$('#datetimepicker').data("DateTimePicker").FUNCTION()

因此,您必须将.data("DateTimePicker")添加到$('#datetimepicker1').disable()

这是一份工作样本:

&#13;
&#13;
$('#datetimepicker1').datetimepicker({
  defaultDate: new Date(),
  format: 'DD/MM/YYYY hh:mm:ss A'
});
$('#datetimepicker1').data("DateTimePicker").disable();
&#13;
<link href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.css" rel="stylesheet"/>
<link href="//cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.47/css/bootstrap-datetimepicker.css" rel="stylesheet"/>

<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.47/js/bootstrap-datetimepicker.min.js"></script>

<div class='input-group date' id='datetimepicker1'>
    <input type='text' class="form-control" />
    <span class="input-group-addon">
        <span class="glyphicon glyphicon-calendar"></span>
    </span>
</div>
&#13;
&#13;
&#13;

答案 2 :(得分:1)

&#13;
&#13;
$(document).ready(function () {
    $('#datetimepicker1').datetimepicker({
        defaultDate: new Date(),
        format: 'DD/MM/YYYY hh:mm:ss A'
    }).find("input:first").prop("disabled", true);
});
&#13;
&#13;
&#13;