JQuery Autocomplete组合框解析

时间:2014-07-19 15:41:54

标签: javascript jquery

我有一个HTML下拉列表,其中包含不同的值,如

STONY BROOK,11790
NY,100010
Tampa,77800

我使用下面的javascript

使用jquery自动完成组合框

我想要完成的是当用户点击组合框中的条目时,第一部分即STONY BROOK应保留在组合框中,下一部分(11790)应填入网页中的另一个文本字段。

知道如何做到这一点?

我做了类似

的事情
var array = city.split(',');

然后尝试做

ui.item.option.value(array[0]);

但是这给了我错误的说法

Uncaught TypeError: string is not a function 

有什么想法吗?

(function( $ ) {
    $.widget( "custom.combobox", {
      _create: function() {
        this.wrapper = $( "<span>" )
          .addClass( "custom-combobox" )
          .insertAfter( this.element );

        this.element.hide();
        this._createAutocomplete();
        this._createShowAllButton();
      },

      _createAutocomplete: function() {
        var selected = this.element.children( ":selected" ),
          value = selected.val() ? selected.text() : "";
          alert("a");
        this.input = $( "<input>" )
          .appendTo( this.wrapper )
          .val( value )
          .attr( "title", "" )
          .addClass( "custom-combobox-input ui-widget ui-widget-content ui-state-default ui-corner-left" )
          .autocomplete({
            delay: 0,
            minLength: 0,
            source: $.proxy( this, "_source" )
          })
          .tooltip({
            tooltipClass: "ui-state-highlight"
          });

        this._on( this.input, {
          autocompleteselect: function( event, ui ) {
              var city=ui.item.option.value;
            var array = city.split(',');
           ui.item.option.value(array[0]);

            ui.item.option.selected = true;

           // $("#combobox:eq(newIndex)").attr("selected", "selected");
//            $("#combobox").next().val($(array[0]));
            alert("b");
            this._trigger( "select", event, {

              item: ui.item.option

            });
          },

          autocompletechange: "_removeIfInvalid"
        });
      },

      _createShowAllButton: function() {
        var input = this.input,
          wasOpen = false;
          alert("c");
        $( "<a>" )
          .attr( "tabIndex", -1 )
          .attr( "title", "Show All Items" )
          .tooltip()
          .appendTo( this.wrapper )
          .button({
            icons: {
              primary: "ui-icon-triangle-1-s"
            },
            text: false
          })
          .removeClass( "ui-corner-all" )
          .addClass( "custom-combobox-toggle ui-corner-right" )
          .mousedown(function() {
            wasOpen = input.autocomplete( "widget" ).is( ":visible" );
          })
          .click(function() {
            alert("d");    
            input.focus();

            // Close if already visible
            if ( wasOpen ) {
             alert("e");
              return;
            }

            // Pass empty string as value to search for, displaying all results
            input.autocomplete( "search", "" );
          });
      },

      _source: function( request, response ) {
        var matcher = new RegExp( $.ui.autocomplete.escapeRegex(request.term), "i" );
        response( this.element.children( "option" ).map(function() {
          var text = $( this ).text();

          if ( this.value && ( !request.term || matcher.test(text) ) )
            return {
              label: text,
              value: text,
              option: this
            };
        }) );
      },

      _removeIfInvalid: function( event, ui ) {
          alert("g");
        // Selected an item, nothing to do
        if ( ui.item ) {
         alert("h");
          return;
        }

        // Search for a match (case-insensitive)
        var value = this.input.val(),
          valueLowerCase = value.toLowerCase(),
          valid = false;
        this.element.children( "option" ).each(function() {
          if ( $( this ).text().toLowerCase() === valueLowerCase ) {

            this.selected = valid = true;
            return false;
          }
        });

        // Found a match, nothing to do
        if ( valid ) {
            alert("a");
          return;
        }

        // Remove invalid value
        this.input
          .val( "" )
          .attr( "title", value + " didn't match any item" )
          .tooltip( "open" );
        this.element.val( "" );
        this._delay(function() {
          this.input.tooltip( "close" ).attr( "title", "" );
        }, 2500 );
        this.input.autocomplete( "instance" ).term = "";
      },

      _destroy: function() {
          alert("a");
        this.wrapper.remove();
        this.element.show();
      }
    });
  })( jQuery );

1 个答案:

答案 0 :(得分:0)

找到实际问题很奇怪,但我找到了解决办法。

我修改了jquery-ui-1.10.3.js中的__response: function (content)。它没有尝试阻止,我添加了try catch,它解决了我的问题。如果有人为此问题找到了正确的解决方案,请在此处更新。

_response: function (content) {

        try{
            var message;
            this._superApply( arguments );
            if ( this.options.disabled || this.cancelSearch ) {
                return;
            }
            if ( content && content.length ) {
                message = this.options.messages.results( content.length );
            } else {
                message = this.options.messages.noResults;
            }
            this.liveRegion.text(message);
        }
        catch (e)
        {
            if (e != "TypeError: string is not a function")
            { alert("Error Occured in jquery-ui-1.10.3.js file at __response function ,error is " + e); }
        }
    }

通过 木克斯