2

我正在使用 eonasdan 日期时间选择器。我通过使用选项选择了默认日期。我使用 chrome、firefox 47.0 和 microsoft edje 25.1 打开了该页面。

  • Firefox 案例:日期显示在文本字段中。但是,如果我转到地址栏并点击“输入”,文本字段中显示的日期就会消失。
  • Chrome 和 edje 案例:当页面打开时,文本字段中不显示日期。

我为这个问题fiddle创建了一个 fiddle,fiddle中发生的事情与 google chrome 和 internet explorer 案例中发生的事情相同。

我尝试使用 datetimepicker 的 'viewDate: true' 选项来解决问题。但它产生了另一个问题——单击日历图标时未显示日历。

我不知道为什么会这样。任何帮助是极大的赞赏。

小提琴中的相同代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta charset="utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
        <title>
                Example
        </title>
        <link rel="stylesheet" type="text/css" media="screen" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" />
        <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css"/>
        <link href="//cdn.rawgit.com/Eonasdan/bootstrap-datetimepicker/e8bddc60e73c1ec2475f827be36e1957af72e2ea/build/css/bootstrap-datetimepicker.css" rel="stylesheet"/>

        <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
        <!--[if lt IE 9]>
            <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
            <script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
        <![endif]-->
        <script type="text/javascript" src="//code.jquery.com/jquery-2.1.1.min.js"></script>
        <script type="text/javascript" src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
        <script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.9.0/moment-with-locales.js"></script>
        <script src="//cdn.rawgit.com/Eonasdan/bootstrap-datetimepicker/e8bddc60e73c1ec2475f827be36e1957af72e2ea/src/js/bootstrap-datetimepicker.js"></script>  
    </head>
    <body>  
        <div class="container"> 
            <form id="j_idt11" name="j_idt11" method="post" action="/myPage.xhtml" enctype="application/x-www-form-urlencoded">
                <input type="hidden" name="j_idt11" value="j_idt11" />          
                <div class="row">
                    <div class="col-md-4 col-md-offset-4">          
                        <div class="panel panel-primary">
                            <div class="panel-heading text-center">
                                My Panel                
                            </div>                  
                            <div class="panel-body">
                                <div class="form-group text-right">                                                                                     
                                    <label class="text-right">My Date</label>           
                                    <div class="form-group">                                                                                                 
                                        <div class='input-group date' id='d0'>
                                            <input type="text" name="j_idt11:j_idt15:0:j_idt21" value="2016-09-03" class="form-control text-right" onkeydown="return false" />
                                            <span class="input-group-addon">
                                                <span class="glyphicon glyphicon-time"></span>
                                            </span>
                                        </div>
                                        <script type="text/javascript">
                                            $(function () {                     
                                                var cID = "#";
                                                cID = cID.concat('d0');

                                                var t = '2016-09-03'

                                                var options = {
                                                        format: 'L',                                
                                                        defaultDate: moment(t)
                                                    };                      

                                                $(cID).datetimepicker(options);                     
                                            });
                                        </script>                
                                    </div>           
                                </div>
                            </div>                                                                  
                        </div>
                    </div>                  
                </div>                          
            </form> 
        </div>
    </body>
</html>
4

2 回答 2

1

您不需要value在 HTML 中输入 datetimepicker 属性。如果您删除它,您的代码将如以下示例所示工作:

var cID = "#";
cID = cID.concat('d0');
var t = '2016-09-03';
var options = {
  format: 'L',
  defaultDate: moment(t)
};                      
$(cID).datetimepicker(options);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.14.1/moment-with-locales.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.37/js/bootstrap-datetimepicker.min.js"></script>

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.37/css/bootstrap-datetimepicker.min.css" rel="stylesheet" />


<div class="container"> 
  <form id="j_idt11" name="j_idt11" method="post" action="/myPage.xhtml" enctype="application/x-www-form-urlencoded">
    <input type="hidden" name="j_idt11" value="j_idt11" />          
    <div class="row">
      <div class="col-md-4 col-md-offset-4">          
        <div class="panel panel-primary">
          <div class="panel-heading text-center">
            My Panel                
          </div>                  
          <div class="panel-body">
            <div class="form-group text-right">
              <label class="text-right">My Date</label>
              <div class="form-group">
                <div class='input-group date' id='d0'>
                  <input type="text" name="j_idt11:j_idt15:0:j_idt21" class="form-control text-right" onkeydown="return false" />
                  <span class="input-group-addon">
                    <span class="glyphicon glyphicon-time"></span>
                  </span>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </form>
</div>

如果要使用数据属性,可以使用data-date-*设置选项或data-date-options设置选项对象。在您的情况下,您可以使用data-date-default-date而不是value.

您可以在此处此处找到有关data-*初始化的更多信息。

于 2016-09-03T09:03:11.240 回答
0

内容分发网络:

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.9.0/moment-with-locales.js"></script>

<script type="text/javascript" src="https://cdn.rawgit.com/Eonasdan/bootstrap-datetimepicker/a549aa8780dbda16f6cff545aeabc3d71073911e/src/js/bootstrap-datetimepicker.js"></script> 

<script type="text/javascript" src="https://cdn.rawgit.com/Eonasdan/bootstrap-datetimepicker/a549aa8780dbda16f6cff545aeabc3d71073911e/build/css/bootstrap-datetimepicker.css"></script>

HTML:

<div class="row">
    <div class="col-md-12">
         <h6>datetimepicker1</h6>

        <div class="form-group">
            <div class="input-group date" id="datetimepicker1">
                <input type="text" class="form-control" />  <span class="input-group-addon"><span class="glyphicon-calendar glyphicon"></span></span>
            </div>
        </div>
         <h6>datetimepicker2</h6>
        <input type="text" class="form-control" id="datetimepicker2" />
    </div>
</div>

JavaScript:

$('#datetimepicker1').datetimepicker();
$('#datetimepicker2').datetimepicker();

使用此代码..

这对你有用..

于 2016-09-03T08:44:31.207 回答