jQuery datepicker点击图标

Mat*_*thi 3 html jquery date datepicker jquery-ui-datepicker

我想在点击输入字段内的FontAwesome图标时显示jQuery datepicker.以下是代码:

<div class="col-md-4 col-sm-4 col-xs-12">
  <label for="datepicker">Date of birth*</label>
  <input type="text" name='datepicker' class="form-control"  value="Select date" id="datepicker" ng-required="true" placeholder="MM/DD/YYYY" >
  <span class="fa fa-calendar"></span>
</div>
Run Code Online (Sandbox Code Playgroud)
jQuery('.fa-calendar').on('click' , function() {
  jQuery(this).datepicker().trigger('focus');
});
Run Code Online (Sandbox Code Playgroud)

Abi*_*Abi 7

试试这个

$(document).ready(function() {
  $("#datepicker").datepicker();
  $('.fa-calendar').click(function() {
    $("#datepicker").focus();
  });
});
Run Code Online (Sandbox Code Playgroud)
  • 仅初始化datepicker一次

这是一个例子

$(document).ready(function() {
  $("#datepicker").datepicker();
  $('.fa-calendar').click(function() {
    $("#datepicker").focus();
  });
});
Run Code Online (Sandbox Code Playgroud)
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <link rel="stylesheet" href="/resources/demos/style.css">
  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<div class="col-md-4 col-sm-4 col-xs-12">
  <label for="datepicker">Date of birth*</label>
  <input type="text" name='datepicker' class="form-control"  value="Select date" id="datepicker" ng-required="true" placeholder="MM/DD/YYYY" >
  <span class="fa fa-calendar"></span>
</div>
Run Code Online (Sandbox Code Playgroud)


小智 5

 <script>
   $(document).on('click',".calendar",function(){
         $("#datepicker").datepicker("show");
     });    
 </script>
Run Code Online (Sandbox Code Playgroud)