Remove Datepicker Function dynamically

后端 未结 6 1523
时光取名叫无心
时光取名叫无心 2020-12-03 02:31

I want to remove datepicker function depending on the dropdownlist selected value. I try the following codes, but it still shows the calendar when I put the cursor in the t

相关标签:
6条回答
  • 2020-12-03 02:56

    Just bind the datepicker to a class rather than binding it to the id . Remove the class when you want to revoke the datepicker...

    $("#ddlSearchType").change(function () { 
      if ($(this).val() == "Required Date" || $(this).val() == "Submitted Date")                   { 
        $("#txtSearch").addClass("mydate");
        $(".mydate").datepicker()
      } else { 
        $("#txtSearch").removeClass("mydate");
      } 
    }); 
    
    0 讨论(0)
  • 2020-12-03 03:02

    Destroy the datepicker's instance when you don't want it and create new instance whenever necessary.

    I know this is ugly but only this seems to be working...

    Check this out

     $("#ddlSearchType").change(function () {
            if ($(this).val() == "Required Date" || $(this).val() == "Submitted Date") {
                    $("#txtSearch").datepicker();
    
             }
              else {
                    $("#txtSearch").datepicker("destroy");                    
             }
     });
    
    0 讨论(0)
  • 2020-12-03 03:08

    Well I had the same issue and tried "destroy" but that not worked for me. Then I found following work around My HTML was:

    <input placeholder="Select Date" id="MyControlId" class="form-control datepicker" type="text" />
    

    Jquery That work for me:

    $('#MyControlId').data('datepicker').remove();
    
    0 讨论(0)
  • 2020-12-03 03:12

    what about using the official API?

    According to the API doc:

    DESTROY: Removes the datepicker functionality completely. This will return the element back to its pre-init state.

    Use:

    $("#txtSearch").datepicker("destroy");

    to restore the input to its normal behaviour and

    $("#txtSearch").datepicker(/*options*/);

    again to show the datapicker again.

    0 讨论(0)
  • 2020-12-03 03:17

    You can try the enable/disable methods instead of using the option method:

    $("#txtSearch").datepicker("enable");
    $("#txtSearch").datepicker("disable");
    

    This disables the entire textbox. So may be you can use datepicker.destroy() instead:

    $(document).ready(function() {
        $("#ddlSearchType").change(function() {
            if ($(this).val() == "Required Date" || $(this).val() == "Submitted Date") {
                $("#txtSearch").datepicker();
            }
            else {
                $("#txtSearch").datepicker("destroy");
            }
        }).change();
    });
    

    Demo here.

    0 讨论(0)
  • 2020-12-03 03:21

    This is the solution I use. It has more lines but it will only create the datepicker once.

    $('#txtSearch').datepicker({
        constrainInput:false,
        beforeShow: function(){
            var t = $('#ddlSearchType').val();
            if( ['Required Date', 'Submitted Date'].indexOf(t) ) {
                $('#txtSearch').prop('readonly', false);
                return false;
            }
            else $('#txtSearch').prop('readonly', true);
        }
    });
    

    The datepicker will not show unless the value of ddlSearchType is either "Required Date" or "Submitted Date"

    0 讨论(0)
提交回复
热议问题