我想在用户在选择框中选择一个值时启用自动完成功能,在用户取消选择时禁用

时间:2022-06-14 19:43:41

1.I want to enable Auto-complete when user select a value in select box and disable when user dis-select

1.我想在用户选择选择框中的值时启用自动完成,并在用户取消选择时禁用

$("#selectorText").autocomplete({
   source: function (request, response) {
      $.getJSON("URL", {
         term: $('#selectorText').val();/this the select box value
      }, response);
   },
   minLength: 1,
   select: function (event, ui) {
       $(this).val(ui.item.value);
   }
});

2.This for default disable auto-complete when Page load

2.这是默认情况下在页面加载时禁用自动完成

$( "#selectorText" ).autocomplete({
   disabled: true; //here is the code default disable
});
  1. now main logic start here, I want to enable Auto complete on special case suppose I have these values in select box {1,2,3,4,5}. and I want to enable auto complete when user select 3 and 4 other wise it will remain disable.
  2. 现在主逻辑从这里开始,我想在特殊情况下启用自动完成,假设我在选择框{1,2,3,4,5}中有这些值。我想在用户选择3和4时启用自动完成,否则它将保持禁用状态。

$('#selctBox').change(function () {
    if(this.val()>0){
         $( "#selectorText" ).autocomplete({
         disabled: true
         });
    }
});

2 个解决方案

#1


You have the solution with you. Your code is fine, just change your condition little bit.

你有解决方案。你的代码很好,只是稍微改变你的条件。

$(document).ready(function(){

    //on page load

       $( "#selectorText" ).autocomplete( "disable" );

    // on selectbox change

    $('#selctBox').change(function () {
        if($(this).val() == 3 || $(this).val() == 4){
           $( "#selectorText" ).autocomplete( "enable" );
        } else {
           $( "#selectorText" ).autocomplete( "disable" );
        }

    });
}); 

#2


$('#selctBox').change(function() {
    var this_val = $(this).val();
    if (3 <= this_val && this_val <= 4 ) {
        $("#selectorText").autocomplete("option", "disabled", false);
    }
    else { // 1,2,5
        $("#selectorText").autocomplete("option", "disabled", true);
    }
});

#1


You have the solution with you. Your code is fine, just change your condition little bit.

你有解决方案。你的代码很好,只是稍微改变你的条件。

$(document).ready(function(){

    //on page load

       $( "#selectorText" ).autocomplete( "disable" );

    // on selectbox change

    $('#selctBox').change(function () {
        if($(this).val() == 3 || $(this).val() == 4){
           $( "#selectorText" ).autocomplete( "enable" );
        } else {
           $( "#selectorText" ).autocomplete( "disable" );
        }

    });
}); 

#2


$('#selctBox').change(function() {
    var this_val = $(this).val();
    if (3 <= this_val && this_val <= 4 ) {
        $("#selectorText").autocomplete("option", "disabled", false);
    }
    else { // 1,2,5
        $("#selectorText").autocomplete("option", "disabled", true);
    }
});