Bootstrap 3 datatimepicker从文本框中清除我的值

时间:2015-04-23 14:19:25

标签: jquery twitter-bootstrap datetimepicker

我的bootstrap datetimepicker有问题。

当我的datatimepicker处于活动状态时(javascript正常),文本字段为空。

当我用id =" ada"更改div的id时并且我的datatimepicker不活动,数据打印正确。它是我的代码MWE(由asp.net mvc用剃刀生成)。

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="Content/bootstrap.css" rel="stylesheet" />
    <link href="Content/site.css" rel="stylesheet" />
    <link href="Content/bootstrap-datetimepicker.css" rel="stylesheet" />

    <script src="Scripts/modernizr-2.8.3.js"></script>

    <script src="Scripts/jquery-2.1.3.js"></script>

    <script src="Scripts/jquery.unobtrusive-ajax.js"></script>
    <script src="Scripts/bootstrap.js"></script>
    <script src="Scripts/respond.js"></script>

    <script src="Scripts/moment-with-locales.js"></script>
    <script src="Scripts/moment.js"></script>
    <script src="Scripts/bootstrap-datetimepicker.js"></script>
</head>
<body>

        <div class="form-group">
            <div class='input-group date datetimepickerbutton' id="ada">
                <input class="form-control" data-val="true" data-val-date="The field DataPoczatku must be a date." data-val-required="The DataPoczatku field is required." id="katalog_DataPoczatku" name="katalog.DataPoczatku" type="date" value="2015-03-01 00:00:00" />

                <span class="input-group-addon">
                    <span class="glyphicon glyphicon-calendar"></span>
                </span>
            </div>
        </div>

    <script type="text/javascript">
    $(document).ready(function () {
        $('#ada').datetimepicker();
    });
    </script>
</body>
</html>

有人知道如何使用活动数据贴纸保持文本框的值吗?

https://jsfiddle.net/abbdzpg2/

2 个答案:

答案 0 :(得分:2)

您应在输入值中指定日期格式YYYY-MM-DD,并在设置中设置格式值。检查此fiddle。这是你想要的结果吗?

$(document).ready(function () {
    $('#ada').datetimepicker({
        format: 'YYYY-MM-DD'
    });
});
<div class="form-group">
    <div class='input-group date datetimepickerbutton' id="ada">
        <input class="form-control" data-val="true" data-val-date="The field DataPoczatku must be a date." data-val-required="The DataPoczatku field is required." id="katalog_DataPoczatku" name="katalog.DataPoczatku" type="date" value="2015-03-01" /> <span class="input-group-addon">
                                <span class="glyphicon glyphicon-calendar"></span>
</span>
    </div>
</div>

答案 1 :(得分:0)

如果要为timepicker设置默认日期,则需要在jQuery脚本中执行此操作,而不是设置值。这是因为add on会覆盖表单控件的默认行为。

var date = new Date('2015-03-01');

$(document).ready(function () {
    $('#ada').datetimepicker({
        defaultDate: date
    });
});