Javascript 禁用 jqueryui 日期选择器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4966998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:59:46  来源:igfitidea点击:

Disable a jqueryui datepicker

javascriptjquery

提问by Prady

I have a jqueryui datepicker associated with a input text. When i make the input textbox readonly i dont want the datepicker to show up . Is that possible? I would be setting the readonly property to true or false from the code.

我有一个与输入文本关联的 jqueryui datepicker。当我将输入文本框设为只读时,我不希望显示日期选择器。那可能吗?我将从代码中将 readonly 属性设置为 true 或 false 。

<script>
    $(function() {
        $( "#datepicker" ).datepicker();
    });
    </script>



<div class="demo">

<p>Date: <input type="text" id="CompleteDate"> <input class="change" type="checkbox" name="chkBoxb" id="chkBoxb"> </p>

</div>


j$('#chkBoxb').click(function(){
   var paramChangeBoxes = j$('input:checkbox.change');

 if (j$(this).is(':checked')) {

  paramChangeBoxes.removeAttr('checked');
    j$('#chkBoxb').attr('checked', 'checked');

     j$('#CompleteDate').attr('readonly',false);
     j$("#CompleteDate").datepicker();


 }
 else
 {
  j$('#CompleteDate').attr('readonly',true);
 $( "#CompleteDate:not([readonly])"  ).datepicker();
 }

});

Updated the code

更新了代码

采纳答案by Jeff the Bear

Here you go:

干得好:

<div class="demo">
  <p>Date: <input type="text" id="CompleteDate"> <input class="change" type="checkbox" name="chkBoxb" id="chkBoxb"> </p>
</div>

<script>
  $('#chkBoxb').click(function(){
   if ($(this).is(':checked')) {
     $('#CompleteDate').attr('readonly',false)
       .datepicker();
   } else {
     $('#CompleteDate').attr('readonly',true)
       .datepicker("destroy");
   }
  });
</script>

回答by BraveNewMath

disable it like this:

像这样禁用它:

$('#dateSelector').datepicker('disable');

and enable it like this:

并像这样启用它:

$('#dateSelector').datepicker('enable');

Here's a working sample: http://jsfiddle.net/CG64h/8/

这是一个工作示例:http: //jsfiddle.net/CG64h/8/

回答by dfl

I ran into this today and unfortunately, the answers provided above did not work. Read the datepicker code, here's how it's done in jqueryui 1.10:

我今天遇到了这个问题,不幸的是,上面提供的答案不起作用。阅读 datepicker 代码,这是它在 jqueryui 1.10 中的完成方式:

$(selector).datepicker("option", "disabled", true);

回答by Anooj

I believe you should use 2 different textbox's and toggle them as needed so that only one is shown at a time. The first one as plain text box and the second one with jquery Datapicker applied.

我相信您应该使用 2 个不同的文本框并根据需要切换它们,以便一次只显示一个。第一个是纯文本框,第二个是应用 jquery Datapicker。

回答by yogeswaran K

if($("#").attr("enabled")=="false") {
  $("#").attr("disabled","true");
}

回答by Christian Rios

Just change the selector and add :enabled

只需更改选择器并添加:enabled

 $( "#datepicker:enabled" ).datepicker();