当前位置: 移动技术网 > IT编程>开发语言>JavaScript > bootstrap日期控件问题(双日期、清空等问题解决)

bootstrap日期控件问题(双日期、清空等问题解决)

2018年05月20日  | 移动技术网IT编程  | 我要评论
bootstrap以它优美的外观和丰富的组件,使它成为目前最流行的前端框架。在项目开发中,我们使用它的日期控件确实遇到了一些问题:     1.日期控件后面两个图标点击触

bootstrap以它优美的外观和丰富的组件,使它成为目前最流行的前端框架。在项目开发中,我们使用它的日期控件确实遇到了一些问题:

    1.日期控件后面两个图标点击触发失效

    2.双日期关联问题

    3.双日期清空时,之前输入日期关联仍然有效

    4.输入年月

    5.图标不显示(这个直接在引入图标的文件里搜url地址,修改地址就可以解决)

  下面的代码都会一一解决。

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="bootstrap.css" rel="external nofollow" type="text/css" />
<link rel="stylesheet" href="bootstrap-datetimepicker.css" rel="external nofollow" type="text/css" />
<script src="jquery-1.11.1.min.js" type="text/javascript"></script>
<script src="bootstrap.js" type="text/javascript" ></script>
<script src="bootstrap-datetimepicker.js" type="text/javascript" ></script>
<script src="bootstrap-datetimepicker.zh-cn.js" type="text/javascript" ></script>
<title>bootstrap日期</title>
<style>
</style>
<script type="text/javascript">
$(function(){
  //输入开始日期和结束日期
  //定位div上的id,不是input上id,否则后面两个小图标会失效
  $("#startdiv").datetimepicker({
    pickerposition : "bottom-left",
    language : 'zh-cn',
    format : "yyyy-mm-dd",
    weekstart : 1,
    todaybtn : 1,
    autoclose : 1,
    todayhighlight : 1,
    startview : 2,
    minview : 2,
    forceparse : 0
  });
  $("#enddiv").datetimepicker({
    pickerposition : "bottom-left",
    language : 'zh-cn',
    format : "yyyy-mm-dd",
    weekstart : 1,
    todaybtn : 1,
    autoclose : 1,
    todayhighlight : 1,
    startview : 2,
    minview : 2,
    forceparse : 0
  });
  //输入年月
  $("#birthmonth").datetimepicker({
    language: 'zh-cn',
    format: 'yyyy-mm',
    autoclose: true,
    // todaybtn: true, 今天提示
    startview: 'year',
    minview:'year',
    maxview:'decade'
  });
  $('#startdiv').unbind("change");
  $('#startdiv').change(function(){
    $('#enddiv').datetimepicker('setstartdate', $("#start").val());
  });
  $('#enddiv').unbind("change");
  $('#enddiv').change(function(){
    $('#startdiv').datetimepicker('setenddate', $("#end").val());
  });
});
function clearform(){
  $('#start').val('');
  $('#end').val('');
  //用于解决清空后,前后日期还会关联的问题
  $('.input-group-addon:has(span.glyphicon-remove)').click();
}
  </script>
</head>
<body>
  <h1>bootstrap日期控件</h1>
  <hr/>
                    <div id="startdiv" class="input-group date width100">
                      <input id="start"
                        name="start" class="form-control" type="text"
                        value="" placeholder="请输入开始日期" readonly="readonly"> <span
                        class="input-group-addon"> <span
                        class="glyphicon glyphicon-remove"></span>
                      </span> <span class="input-group-addon"> <span
                        class="glyphicon glyphicon-calendar"></span>
                      </span>
                    </div>
                    <br>
                    <div id="enddiv" class="input-group date width100">
                      <input id="end"
                        name="end" class="form-control" type="text"
                        value="" placeholder="请输入结束日期" readonly="readonly"> <span
                        class="input-group-addon"> <span
                        class="glyphicon glyphicon-remove"></span>
                      </span> <span class="input-group-addon"> <span
                        class="glyphicon glyphicon-calendar"></span>
                      </span>
                    </div>
                    <br>
                    <button type="button" class="btn btn-sm btn-warning" id="clear" onclick="clearform()">清空</button>
                    <hr>
                    <div id="birthmonth" class="input-group date width100">
                      <input id="birthday"
                        name="birthday" class="form-control" type="text"
                        value="" placeholder="请输入出生年月" readonly="readonly"> <span
                        class="input-group-addon"> <span
                        class="glyphicon glyphicon-remove"></span>
                      </span> <span class="input-group-addon"> <span
                        class="glyphicon glyphicon-calendar"></span>
                      </span>
                    </div>
</body>
</html>

以上所述是小编给大家介绍的bootstrap日期控件问题(双日期、清空等问题解决),希望对大家有所帮助

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网