我想在用户 select 框中的值 select 时启用自动完成功能,并在用户取消 select 时禁用

I want to enable Autocomplete when user select a value in select box and disable when user dis-select

1.I 想要在用户 select 在 select 框中输入值时启用自动完成功能,并在用户取消 select

时禁用
$("#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 默认禁用页面加载时自动完成

$( "#selectorText" ).autocomplete({
   disabled: true; //here is the code default disable
});
  1. 现在主要逻辑从这里开始,我想在特殊情况下启用自动完成假设我在 select 框 {1,2,3,4,5} 中有这些值。我想在用户 select 3 和 4 时启用自动完成,否则它将保持禁用状态。
$('#selctBox').change(function () {
    if(this.val()>0){
         $( "#selectorText" ).autocomplete({
         disabled: true
         });
    }
});
$('#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);
    }
});

你有解决办法。你的代码没问题,只是稍微改变一下你的条件。

$(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" );
        }

    });
});