jquery autocomplete组合框错误:未捕获类型错误:Object[Object Object]没有方法&#

jquery autocomplete combobox error: Uncaught TypeError: Object [object Object] has no method 'button'

本文关键字:Object 错误 有方法 组合 autocomplete jquery 类型      更新时间:2023-09-26

我正在尝试使用jquery ui插件实现一个自动完成组合框

使用下面提到的代码,我能够实现自动完成部分,但不能实现下拉部分(由于未捕获的类型错误,下拉箭头不可见)

$.widget( "ui.combobox", {
        _create: function() {
            var input,
                self = this,
                select = this.element.hide(),
                selected = select.children( ":selected" ),
                value = selected.val() ? selected.text() : "",
                wrapper = this.wrapper = $( "<span>" )
                    .addClass( "ui-combobox" )
                    .insertAfter( select );
            input = $( "<input>" )
                .appendTo( wrapper )
                .val( value )
                .addClass( "ui-state-default ui-combobox-input" )
                .autocomplete({
                    delay: 0,
                    minLength: 0,
                    source: function( request, response ) {
                        var matcher = new RegExp( $.ui.autocomplete.escapeRegex(request.term), "i" );
                        response( select.children( "option" ).map(function() {
                            var text = $( this ).text();
                            if ( this.value && ( !request.term || matcher.test(text) ) )
                                return {
                                    label: text.replace(
                                        new RegExp(
                                            "(?![^&;]+;)(?!<[^<>]*)(" +
                                            $.ui.autocomplete.escapeRegex(request.term) +
                                            ")(?![^<>]*>)(?![^&;]+;)", "gi"
                                        ), "<strong>$1</strong>" ),
                                    value: text,
                                    option: this
                                };
                        }) );
                    },
                    select: function( event, ui ) {
                        ui.item.option.selected = true;
                        self._trigger( "selected", event, {
                            item: ui.item.option
                        });
                    },
                    change: function( event, ui ) {
                        if ( !ui.item ) {
                            var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( $(this).val() ) + "$", "i" ),
                                valid = false;
                            select.children( "option" ).each(function() {
                                if ( $( this ).text().match( matcher ) ) {
                                    this.selected = valid = true;
                                    return false;
                                }
                            });
                            if ( !valid ) {
                                // remove invalid value, as it didn't match anything
                                $( this ).val( "" );
                                select.val( "" );
                                input.data( "autocomplete" ).term = "";
                                return false;
                            }
                        }
                    }
                })
                .addClass( "ui-widget ui-widget-content ui-corner-left" );
            input.data( "autocomplete" )._renderItem = function( ul, item ) {
                return $( "<li></li>" )
                    .data( "item.autocomplete", item )
                    .append( "<a>" + item.label + "</a>" )
                    .appendTo( ul );
            };
            $( "<a>" )
                .attr( "tabIndex", -1 )
                .attr( "title", "Show All Items" )
                .appendTo( wrapper )
                .button({
                    icons: {
                        primary: "ui-icon-triangle-1-s"
                    },
                    text: false
                })
                .removeClass( "ui-corner-all" )
                .addClass( "ui-corner-right ui-combobox-toggle" )
                .click(function() {
                    // close if already visible
                    if ( input.autocomplete( "widget" ).is( ":visible" ) ) {
                        input.autocomplete( "close" );
                        return;
                    }
                    // work around a bug (likely same cause as #5265)
                    $( this ).blur();
                    // pass empty string as value to search for, displaying all results
                    input.autocomplete( "search", "" );
                    input.focus();
                });
        },
        destroy: function() {
            this.wrapper.remove();
            this.element.show();
            $.Widget.prototype.destroy.call( this );
        }
    });

    $( "#combobox" ).combobox();

以上代码在document.ready下。由于".button"方法而引发错误

html:

        <tr>
            <td><label>Country:</label></td>
            <td>
            <div class="ui-widget">
                    <select id="combobox">
                        <option value="">Select one...</option>
                          <option value="ActionScript">ActionScript</option>
                          <option value="AppleScript">AppleScript</option>
                          <option value="Asp">Asp</option>
                          <option value="BASIC">BASIC</option>
                          <option value="C">C</option>      
                    </select>
            </div>
            </td>
        </tr>

组合框css,

.ui-combobox {
    position: relative;
    display: inline-block;
}
.ui-combobox-toggle {
    position: absolute;
    top: 0;
    bottom: 0;
    margin-left: -1px;
    padding: 0;
    /* adjust styles for IE 6/7 */
    *height: 1.7em;
    *top: 0.1em;
}
.ui-combobox-input {
    margin: 0;
    padding: 0.3em;
}

脚本序列为,

  <script type="text/javascript" src="<?php echo base_url('/assets/js/jquery.js'); ?>"></script>
    <script type="text/javascript" src="<?php echo base_url('/assets/js/validation.js'); ?>"></script>
    <script type="text/javascript" src="<?php echo base_url('/assets/autocomplete/js/jquery-ui-1.8.21.custom.min.js'); ?>"></script>

    <?= $_scripts ?>
  • jquery版本1.7.2,jquery ui版本1.8.21
  • 我试着重新安排剧本的顺序
  • 不存在不同jquery版本的多个实例

如有任何帮助,我们将不胜感激。谢谢

您的代码看起来不错,所以问题几乎可以肯定是您的自定义jQuery UI构建中没有包含ui.button

您可以通过运行typeof $.ui.button来验证这一点。如果您包含了它,它将是function,如果您不包含,它将为undefined

重新构建jQuery UI,并确保选中Button复选框。