Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么自动完成小部件没有';如果文本不为空,则无法工作?_Javascript_Jquery - Fatal编程技术网

Javascript 为什么自动完成小部件没有';如果文本不为空,则无法工作?

Javascript 为什么自动完成小部件没有';如果文本不为空,则无法工作?,javascript,jquery,Javascript,Jquery,如果文本为空,下面的代码怎么可能只显示数组。。。它应该在键入时显示列表,而不是在文本为空时 步骤: 关注文本 键入一个文本,如“abc”(此文本返回一个执行ajax调用的数组) 什么也没发生 删除“abc”,我就能正确地看到数组 $('#titolare').keyup(function(){ var titolare = $.trim($('#titolare').val()); $.ajax({ type: "POST", url: "page.php",

如果文本为空,下面的代码怎么可能只显示数组。。。它应该在键入时显示列表,而不是在文本为空时

步骤:

  • 关注文本
  • 键入一个文本,如“abc”(此文本返回一个执行ajax调用的数组)
  • 什么也没发生
  • 删除“abc”,我就能正确地看到数组

    $('#titolare').keyup(function(){
      var titolare = $.trim($('#titolare').val());
    
      $.ajax({
        type: "POST",
        url: "page.php",
        data: { titolare: titolare },
        success: function(msg){          
          var obj = jQuery.parseJSON(msg);
    
          if (obj.result){
            var tit   = obj.titolare
              , tit_a = [];            
    
            $.each (tit, function (a) {              
              tit_a[a] = { titolare: tit[a].titolare, cod_fis: tit[a].cod_fis };
            });         
    
                $("#titolare").autocomplete({
              minLength: 0,         
              source: tit_a,
                    focus: function( event, ui ) {             
                        $("#titolare").val(ui.item.titolare);
                        return false;
                    },
                    select: function( event, ui ) {            
                        $("#titolare").val(ui.item.titolare);
                        return false;
                    }
                })
                .data( "autocomplete" )._renderItem = function(ul, item) {          
                    return $( "<li></li>" )
                        .data( "item.autocomplete", item )
                        .append( "<a>" + item.titolare + "<br />("+ item.cod_fis +")</a>")
                        .appendTo( ul );
                };            
    
          }                                             
        }
      });        
    });
    
    $('#titolare').keyup(函数(){
    var titolare=$.trim($('#titolare').val());
    $.ajax({
    类型:“POST”,
    url:“page.php”,
    数据:{titolare:titolare},
    成功:函数(msg){
    var obj=jQuery.parseJSON(msg);
    如果(目标结果){
    var tit=对象titolare
    ,tit_a=[];
    $.each(tit,function(a){
    tit_a[a]={titolare:tit[a]。titolare,cod_fis:tit[a]。cod_fis};
    });         
    $(“#titolare”)。自动完成({
    最小长度:0,
    资料来源:tit_a,
    焦点:函数(事件、ui){
    $(“#titolare”).val(ui.item.titolare);
    返回false;
    },
    选择:函数(事件,ui){
    $(“#titolare”).val(ui.item.titolare);
    返回false;
    }
    })
    .data(“自动完成”)。\u renderItem=函数(ul,项){
    返回$(“
  • ”) .data(“item.autocomplete”,item) .追加(“+item.titolare+”
    (“+item.cod\u fis+”) .附录(ul); }; } } }); });

  • 您不想使用
    keyUp
    功能。您可以像下面这样提供url作为源

    $(document).ready(function(){
        $( "#titolare" ).autocomplete({
                 source: "page.php",                }
                 focus: function( event, ui ) {             
                            $("#titolare").val(ui.item.titolare);
                            return false;
                        },
                        select: function( event, ui ) {            
                            $("#titolare").val(ui.item.titolare);
                            return false;
                        }
        }).data( "autocomplete" )._renderItem = function(ul, item) {          
                        return $( "<li></li>" )
                            .data( "item.autocomplete", item )
                            .append( "<a>" + item.titolare + "<br />("+ item.cod_fis +")</a>")
                            .appendTo( ul );
                    };  
    });
    
    $(文档).ready(函数(){
    $(“#titolare”)。自动完成({
    来源:“page.php”,}
    焦点:函数(事件、ui){
    $(“#titolare”).val(ui.item.titolare);
    返回false;
    },
    选择:函数(事件,ui){
    $(“#titolare”).val(ui.item.titolare);
    返回false;
    }
    }).data(“自动完成”)。\u renderItem=函数(ul,项){
    返回$(“
  • ”) .data(“item.autocomplete”,item)
    .append(“

    您不想使用
    keyUp
    函数。您可以这样给url作为源

    $(document).ready(function(){
        $( "#titolare" ).autocomplete({
                 source: "page.php",                }
                 focus: function( event, ui ) {             
                            $("#titolare").val(ui.item.titolare);
                            return false;
                        },
                        select: function( event, ui ) {            
                            $("#titolare").val(ui.item.titolare);
                            return false;
                        }
        }).data( "autocomplete" )._renderItem = function(ul, item) {          
                        return $( "<li></li>" )
                            .data( "item.autocomplete", item )
                            .append( "<a>" + item.titolare + "<br />("+ item.cod_fis +")</a>")
                            .appendTo( ul );
                    };  
    });
    
    $(文档).ready(函数(){
    $(“#titolare”)。自动完成({
    来源:“page.php”,}
    焦点:函数(事件、ui){
    $(“#titolare”).val(ui.item.titolare);
    返回false;
    },
    选择:函数(事件,ui){
    $(“#titolare”).val(ui.item.titolare);
    返回false;
    }
    }).data(“自动完成”)。\u renderItem=函数(ul,项){
    返回$(“
  • ”) .data(“item.autocomplete”,item)
    .append(

    您可以使用
    #titolare
    中的autocomplete小部件(无需使用
    keyup
    侦听器)将
    源属性设置为一个函数,该函数将执行所需的ajax调用和数据转换(现在您尝试使用
    keyup
    方法)

    比如:

    $("#titolare").autocomplete({
          minLength: 0,         
          source: function(request, response){
              // ajax call and any data transformation here...
          },
          focus: function( event, ui ) {             
             $("#titolare").val(ui.item.titolare);
             return false;
          },
          select: function( event, ui ) {            
             $("#titolare").val(ui.item.titolare);
             return false;
             }
     })
     ...
    

    看看。

    您可以使用
    #titolare
    中的autocomplete小部件(无需使用
    keyup
    侦听器)将
    source
    属性设置为一个函数,该函数将执行所需的ajax调用和数据转换(现在您尝试使用
    keyup
    方法)

    比如:

    $("#titolare").autocomplete({
          minLength: 0,         
          source: function(request, response){
              // ajax call and any data transformation here...
          },
          focus: function( event, ui ) {             
             $("#titolare").val(ui.item.titolare);
             return false;
          },
          select: function( event, ui ) {            
             $("#titolare").val(ui.item.titolare);
             return false;
             }
     })
     ...
    
    看一看