如何通过单击按钮打开引导日期选择器?

时间:2019-06-11 16:49:00

标签: datepicker bootstrap-4

我有一个引导日期选择器,可以通过单击输入来打开。我想更改它并通过单击一个按钮来打开它。如何执行?

这是我的代码,可在输入中打开日期选择器:

<!DOCTYPE html>
<html>
<head>
<title>Test Zone</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <meta charset="utf-8">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"/>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.4.1/css/bootstrap-datepicker3.css"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script src="http://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.min.js"></script>

<body>
    <div class="bootstrap-iso">
     <div class="container-fluid">
      <div class="row">
       <div class="col-md-6 col-sm-6 col-xs-12">

        <!-- Form code begins -->
        <form method="post">
          <div class="form-group"> <!-- Date input -->
            <label class="control-label" for="date">Date</label>
            <input class="form-control" id="date" name="date" placeholder="MM/DD/YYY" type="text"/>
          </div>
          <div class="form-group"> <!-- Submit button -->
            <button class="btn btn-primary " id="submit" type="submit">Submit</button>
          </div>
         </form>
         <!-- Form code ends --> 

        </div>
      </div>    
     </div>
    </div>

<script>

    $(document).ready(function(){
          var date_input=$('input[name="date"]'); //our date input has the name "date"
          var container=$('.bootstrap-iso form').length>0 ? $('.bootstrap-iso form').parent() : "body";
          var options={
            format: 'mm/dd/yyyy',
            container: container,
            todayHighlight: true,
            autoclose: true,
          };
          date_input.datepicker(options);
    })
</script>


</body>
</html>

现在,如何才能在输入时通过单击submit按钮而不是单击input来强制打开日期选择器?

预先感谢

3 个答案:

答案 0 :(得分:0)

将以下代码添加到脚本标签中

 $("#submit").click(function(){
    var date_input=$('input[name="date"]'); //our date input has the name "date"
    date_input.focus();
 })

答案 1 :(得分:0)

尝试一下。 使用e.preventDefault(); $("#date").datepicker().focus(); 添加

$("#submit").click(function(e){
   e.preventDefault();
   $("#date").datepicker().focus();
 })

<!DOCTYPE html>
<html>
<head>
<title>Test Zone</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <meta charset="utf-8">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"/>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.4.1/css/bootstrap-datepicker3.css"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script src="http://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.min.js"></script>
</head>
<body>
    <div class="bootstrap-iso">
     <div class="container-fluid">
      <div class="row">
       <div class="col-md-6 col-sm-6 col-xs-12">

        <!-- Form code begins -->
        <form method="post" id="form">
          <div class="form-group"> <!-- Date input -->
            <label class="control-label" for="date">Date</label>
            <input class="form-control" id="date" name="date" placeholder="MM/DD/YYY" type="text"/>
          </div>
          <div class="form-group"> <!-- Submit button -->
            <button class="btn btn-primary " id="submit" type="submit">Submit</button>
          </div>
         </form>
         <!-- Form code ends --> 

        </div>
      </div>    
     </div>
    </div>

<script>

    $(document).ready(function(){
          var date_input=$('input[name="date"]'); //our date input has the name "date"
          var container=$('.bootstrap-iso form').length>0 ? $('.bootstrap-iso form').parent() : "body";
          var options={
            format: 'mm/dd/yyyy',
            container: container,
            todayHighlight: true,
            autoclose: true,
          };
          date_input.datepicker(options);
    })
    $("#submit").click(function(e){
     e.preventDefault();
     $("#date").datepicker().focus();
 	})

</script>

</body>
</html>

答案 2 :(得分:0)

我没有使用bootstraps datepicker,而是使用chrome和firefox中的本机datepickers

我正在使用以下技巧:

希望他们能帮助您适应自举程序

div {
  position: relative;
}
input {
  position: absolute;
  opacity: 0;
}
input::-webkit-calendar-picker-indicator {
  width: 100%;
  height: 100%;
  opacity: 0;
}
input:hover + button {
  background-color: silver;
}
<div>
  <input type="date">
  <button id="calendar_text">Search Date ?</button>
</div>