【问题标题】:I want to enable Autocomplete when user select a value in select box and disable when user dis-select我想在用户在选择框中选择一个值时启用自动完成,并在用户取消选择时禁用
【发布时间】:2015-05-05 11:27:53
【问题描述】:

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

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

【问题讨论】:

    标签: php jquery autocomplete


    【解决方案1】:

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

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

      【讨论】:

        猜你喜欢
        • 2013-12-02
        • 2015-09-01
        • 2017-10-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-22
        • 2013-09-13
        • 2011-11-21
        相关资源
        最近更新 更多