【问题标题】:Clear textbox after auto complete select in jquery在jquery中自动完成选择后清除文本框
【发布时间】:2012-10-28 07:14:28
【问题描述】:

我已经使用 JQuery 实现了自动完成,当我选择数据时,它会存储在结果表中,选择后,我需要清除我无法清除的自动完成文本框。搜索相同并得到这个Clear text box in Jquery Autocomplete after selection,但我不知道将它放在哪里,而且在萤火虫中,我在函数(事件,ui)处出错。 请帮助...我的代码如下。

$(function() {
    function log( message ) {


        $( "<div>" ).text( message ).prependTo( "#log" ).click(function(o){
            $(this).remove();
        });
        $( "#log" ).scrollTop( 0 );

    }


    $( "#poolName" ).autocomplete({
        source: function( request, response ) {
            $.ajax({
                url: "/DataWeb/getPoolName",
                type : 'post',
                dataType: 'json',
                data: { name_startsWith: request.term },
                success: function( data ) {
                    console.log(data);
                     response( $.map( data, function( item ) {
                        return {
                             label: item.poolName,
                             value: item.poolName
                        }
                    })); 
                }
            });
        },
        minLength: 1,
        select: function( event, ui ) {
            log( ui.item ?
                  ui.item.label :
                "Nothing selected, input was " + this.value);
        },
        open: function() {
            $( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
        },
        close: function() {
            $( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
        }

    });

});

【问题讨论】:

    标签: jquery jquery-ui jquery-autocomplete


    【解决方案1】:
    $( "#poolName" ).autocomplete({
        source: function( request, response ) {
            $.ajax({
                url: "/DataWeb/getPoolName",
                type : 'post',
                dataType: 'json',
                data: { name_startsWith: request.term },
                success: function( data ) {
                    console.log(data);
                     response( $.map( data, function( item ) {
                        return {
                             label: item.poolName,
                             value: item.poolName
                        }
                    })); 
                }
            });
        },
        select: function (e, i) {
           $('#poolName').val('');   
           return false;    
        }
    
        ,minLength: 1,
        select: function( event, ui ) {
            log( ui.item ?
                  ui.item.label :
                "Nothing selected, input was " + this.value);
        },
        open: function() {
            $( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
        },
        close: function() {
            $( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
        }
    
    });
    
    });
    

    【讨论】:

    • 您可以粘贴您尝试过的代码或创建一个 jsfiddle(ttp://jsfiddle.net/) 以便我们检查您的代码
    • 我没有收到任何错误,但文本框在选择时不会清除
    【解决方案2】:

    尝试在 select: 子句中使用 return false;,

    select: function( event, ui ) {
                log( ui.item ?
                      ui.item.label :
                    "Nothing selected, input was " + this.value);
                return false;
            },
    

    【讨论】:

    • 它对我来说很好用,但为什么呢?作为 javascript 新手,我不知道。
    【解决方案3】:

    我试过这个 document.getElementById("poolName").value="";它正在工作。

    function log( message ) {
            document.getElementById("poolName").value="";
    
            $( "<div>" ).text( message ).prependTo( "#log" ).click(function(o){
                $(this).remove();
            });
            $( "#log" ).scrollTop( 0 );
    
        }
    

    【讨论】:

      【解决方案4】:

      其他答案的问题是它没有 preventDefault 调用。

      这将起作用:

      select: function (e, i) {
         e.preventDefault();
         $('#poolName').val('');   
         return false;    
      }
      

      【讨论】:

        【解决方案5】:

        我发现我必须同时清除字段并返回 false

        select: function(ev, ui) {
          console.log(this, ui.item)
          $(this).val('')
          return false
        },
        

        【讨论】:

          猜你喜欢
          • 2012-07-21
          • 2022-10-21
          • 1970-01-01
          • 2018-02-26
          • 1970-01-01
          • 2011-02-03
          • 2019-02-25
          • 1970-01-01
          相关资源
          最近更新 更多