jQuery datepicker on click of icon

2019-05-14 23:08发布

I want to show the jQuery datepicker on click of a FontAwesome icon which is inside the input field. Below is the code:

<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>
jQuery('.fa-calendar').on('click' , function() {
  jQuery(this).datepicker().trigger('focus');
});

6条回答
放我归山
2楼-- · 2019-05-14 23:12

You can use the 'open' setting on a datepicker to programmatically open it. There's no need to trigger a focus event. You also need to call the datepicker() method on the #datepicker element, not the clicked icon. Try this:

jQuery(function($) {
  $('#datepicker').datepicker();

  $('.fa-calendar').on('click', function() {
    $('#datepicker').datepicker('open');
  });
});
查看更多
Viruses.
3楼-- · 2019-05-14 23:17

  $( function() {
    $( "#datepicker" ).datepicker({
      showOn: "button",
      buttonImage: "images/calendar.gif",
      buttonImageOnly: true,
      buttonText: "icono",
showOn: "both"
    });
  } );
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>jQuery UI Datepicker - Icon trigger</title>
  <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>
 
</head>
<body>
 
<p>Date: <input type="text" id="datepicker"></p>
 
 
</body>
</html>

查看更多
男人必须洒脱
4楼-- · 2019-05-14 23:21

HTML

<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_val" ng-required="true" placeholder="MM/DD/YYYY" >
        <span class="fa fa-calendar"></span>
</div>

JS

jQuery(".fa-calendar").datepicker();

This is what you want to do. It will work, you can fill date in your input type like below.

jQuery(".fa-calendar").datepicker().on('changeDate', function(ev){

            jQuery("#datepicker_val").val(ev.date.valueOf());

        }
    });
查看更多
迷人小祖宗
5楼-- · 2019-05-14 23:28

You want to trigger the datepicker on the input box, not the icon, so you need to change

jQuery(this).datepicker().trigger('focus');`

to be

jQuery("#datepicker").datepicker().trigger('focus');
查看更多
倾城 Initia
6楼-- · 2019-05-14 23:32
 <script>
   $(document).on('click',".calendar",function(){
         $("#datepicker").datepicker("show");
     });    
 </script>
查看更多
三岁会撩人
7楼-- · 2019-05-14 23:34

Try this

$(document).ready(function() {
  $("#datepicker").datepicker();
  $('.fa-calendar').click(function() {
    $("#datepicker").focus();
  });
});
  • Initialise datepicker once only

Here is the example

$(document).ready(function() {
  $("#datepicker").datepicker();
  $('.fa-calendar').click(function() {
    $("#datepicker").focus();
  });
});
<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>

查看更多
登录 后发表回答