javascript - jQuery UI 自动完成组合框选项需要在 iPhone 上双击

标签 javascript jquery ios cordova combobox

我使用 Cordova 创建了一个应用程序,其中包含 jQuery 自定义自动完成组合框。它在浏览器以及物理 android 设备上都能完美运行。然而,现在我将应用程序放在 iPhone 上,我遇到了一个小错误,这对于最终用户来说可能非常烦人。

错误: 一旦选项从组合框显示给用户,他们必须单击一次,突出显示他们的选择,然后第二次单击以实际进行选择。在 Android 设备上的预期用途是仅单击选项一次即可进行选择。

任何点击之前 /image/CEKbT.png

首次点击 /image/JEAfo.png

第一次单击应该进行选择,但只是突出显示该选项并等待用户再次单击突出显示的选项。同样的问题也发生在 iPhone 模拟器和物理设备上,所以我不认为这是特定于设备的。

这是HTML

<div class="ui-widget"><select id="testCombo"></select></div>

Js将其变成ComboBox

$("#testCombo").combobox()

这是用于组合框的自定义小部件

(function( $ ) {
$.widget( "custom.combobox", {
    options: {
        'userChanged': function() {
    //@Overridden if needed
        }
},

_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() : "";

        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 ) {
        ui.item.option.selected = true;
        this._trigger( "select", event, {
        item: ui.item.option
        });
    },

    //* Remove commented code below so that if User types
    //a value not in options, it gets removed 
    //autocompletechange: "_removeIfInvalid"
    autocompletechange: "userChanged"
        });
},

_createShowAllButton: function() {
        var input = this.input,
    wasOpen = false;

        $( "<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() {
        input.focus();

        // Close if already visible
        if ( wasOpen ) {
        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 ) {

        // Selected an item, nothing to do
        if ( ui.item ) {
    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 ) {
    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() {
        this.wrapper.remove();
        this.element.show();
},

//set value for combobox
autocomplete : function(value) {
    this.element.val(value);
    this.input.val(value);      
},

//set the placeholder of the combobox
placeholder : function(value) {
    this.element.attr("placeholder",value);
    this.input.attr("placeholder",value);
},

//used to get the custom typed text from the autoCombo
getval: function(){
    var value = this.input.val();
    if (value === ""){
    return null;
    }else{
    return value;
    }
},

userChanged: function() {
    if($.isFunction(this.options.userChanged))
            this.options.userChanged();
    }

});
})( jQuery );

最佳答案

原因可能与工具提示有关。 iPhone首先执行悬停事件,然后下一次单击才是真正的单击事件。我遇到了同样的问题,删除“悬停”项目解决了我的问题。

关于javascript - jQuery UI 自动完成组合框选项需要在 iPhone 上双击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32099619/

相关文章:

javascript - igUpload 上传大文件失败

javascript - 如何在 Javascript 中对数组值使用 split() 方法?

javascript - 如果 DOM 的滚动位置大于 xyz,则显示元素

JavaScript document.querySelector() 与 jQuery $() 方法相同吗?

javascript - 如何将一个div的内容复制到另一个div

iOS 内存管理工具

ios - 检查 Array 中的 Int 。 swift 3,iOS

ios - NSData 不是 NSData 的子类型吗?

javascript - Google Analytics - GA 在删除 Cookie 后如何跟踪用户?

javascript - YouTube API 凭据 - 未捕获的 TypeError : Cannot read property '0' of undefined