简体   繁体   English

输入框为只读时如何禁用Datepicker

[英]How to Disable Datepicker when the input box is readonly

How to turn off datepicker when input box is in readonly? 输入框处于只读状态时如何关闭日期选择器? Here is my textbox 这是我的文字框

<input type="text" name="letter_date" value="12/05/2019"  id="letter_date" class="form-control datepicker" placeholder="Masukkan Tanggal Nota" data-toggle="datepicker" readonly="readonly" />

and I have tried using this javascript code but when I select the textbox it is still showing datepicker. 并且我尝试使用此javascript代码,但是当我选择文本框时,它仍显示datepicker。

$('[data-toggle="datepicker"]').datepicker({
    beforeShow: function(i) {
      if ($(this).prop('readonly')) {
        e.preventDefault();
        e.stopImmediatePropagation();
        return false;
        }
    },
    format : 'dd/mm/yyyy',
    autoHide : true
});

You can disable the datepicker with datepicker("option", "disabled", true); 您可以使用datepicker("option", "disabled", true);禁用datepicker datepicker("option", "disabled", true); .

 $('[data-toggle="datepicker"]').datepicker({ beforeShow: function(i) { if ($(this).prop('readonly')) { console.log('readonly is true'); $(this).datepicker("option", "disabled", true); return; } }, format : 'dd/mm/yyyy', autoHide : true, }); 
 <link href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylesheet"/> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <input type="text" name="letter_date" value="12/05/2019" id="letter_date" class="form-control datepicker" placeholder="Masukkan Tanggal Nota" data-toggle="datepicker" readonly="readonly"/> 

--Edit-- - 编辑 -

For multiple inputs 对于multiple输入

 $('[data-toggle="datepicker"]').datepicker({ beforeShow: function(i) { if ($(this).prop('readonly')) { $(this).datepicker("option", "disabled", true); return; } }, format : 'dd/mm/yyyy', autoHide : true, }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <link href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylesheet"/> <input type="text" name="letter_date" value="12/05/2019" id="letter_date" class="form-control datepicker" placeholder="Masukkan Tanggal Nota" data-toggle="datepicker" readonly/> <input type="text" name="letter_date" value="12/05/2019" id="letter_date1" class="form-control datepicker" placeholder="Masukkan Tanggal Nota" data-toggle="datepicker"/> <input type="text" name="letter_date" value="12/05/2019" id="letter_date2" class="form-control datepicker" placeholder="Masukkan Tanggal Nota" data-toggle="datepicker" readonly/> 

Try below Code : 试试下面的代码:

$('[data-toggle="datepicker"]').datepicker({
    beforeShow: function(i) {
      if ($(this).prop('readonly')) {
        e.preventDefault();
        e.stopImmediatePropagation();
        return false;
        }
    },
    format : 'dd/mm/yyyy',
    autoHide : true,
    disabled: true
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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