简体   繁体   中英

Disabling datepicker on an input field

Cuurently I have an input field using datepicker . I am wondering how to disable the datepicker callendar popping up when I click on that field, since on my page I want the date option to be available only on certain conditions.

<input id="date" type="text " class="form-control datepicker" name="date" value="Click to select date">

I tried with :

 $("#date").datepicker('disable');

And with :

$( "#date" ).datepicker( "disabled", true );

None worked.

One of the easiest way is to remove all the pointer events for that input field.

$("#date").css('pointer-events', 'none');

This will keep the data intact if you want.

And to re enable pointer events.

   $("#date").css("pointer-events", "");

This seems to be working:

$('#date').datepicker('option', 'disabled', t_or_f)

Working example:

 function toggleDatePicker (state) { // this actually does the magic $('#date').datepicker('option', 'disabled', !state) } $(document).ready(function() { // init $('#date').datepicker(); // ignore this, just for the sake of example $('#butt').on('click', function() { var st = parseInt($(this).data('state')); $(this).data('state', st = 1 - st); // toggle 0 and 1 $(this).text(st ? 'Disable' : 'Enable'); toggleDatePicker(Boolean(st)); }); }); 
 @import url(https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script src="http://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script> <input id="date" type="text " class="form-control datepicker" name="date" value="Click to select date"> <button id="butt" data-state="1">Disable</button> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM