有没有办法只使用文本框显示年份?

时间:2016-09-24 10:48:52

标签: jquery bootstrap-modal

我正在使用bootstrap datepicker,但它只显示日期,月份和年份,我只想在文本框中的datepicker显示年份

1 个答案:

答案 0 :(得分:2)

试试这个:

$("#datepicker").datepicker( {
    format: " yyyy",  //see there is extra space in format, dont remove it
    viewMode: "years", 
    minViewMode: "years"
});

使用它:

<html lang="en">

<head>


    <title>Bootstrap - year picker only example</title>

    <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-datepicker/1.5.0/css/bootstrap-datepicker.css" rel="stylesheet">

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.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-datepicker/1.5.0/js/bootstrap-datepicker.js"></script>


</head>

<body>


<div class="container text-center">


     <h2>Bootstrap - year picker only example</h2>

     <input class="date-own form-control" style="width: 300px;" type="text">


  <script type="text/javascript">

      $('.date-own').datepicker({

         minViewMode: 2,

         format: 'yyyy'

       });

  </script>


</div>


</body>

</html>