选择的插件似乎不适用于移动浏览器

Bol*_*sar 32 javascript jquery android jquery-chosen

我为选择字段设置了一个Chosen插件,以便用户能够从长列表中进行类型搜索.

虽然我正在为手机开发这款产品,虽然它在计算机上运行良好,但它在Apple和Android手机上都被禁用,弹出的默认用户界面用于选择输入.

我想在手机上使用这个插件.

dre*_*ver 33

在使用任何插件之前,请尝试检查其范围.

Android或IOS不支持选择,"在iPhone,iPod Touch和Android移动设备上禁用选择"

在这里查看官方CHOSEN链接

  • [selectize](http://brianreavis.github.io/selectize.js/)是适用于移动设备的替代方案 (4认同)

Abo*_*ang 16

功能browser_is_supportedchosen.jquery.js说明,它有意避免激活iPhone和Android平台(因为几个UX问题).但你可以自己破解它.

 AbstractChosen.browser_is_supported = function() {
  if (window.navigator.appName === "Microsoft Internet Explorer") {
    return document.documentMode >= 8;
  }
  if (/iP(od|hone)/i.test(window.navigator.userAgent)) {
    return false;
  }
  if (/Android/i.test(window.navigator.userAgent)) {
    if (/Mobile/i.test(window.navigator.userAgent)) {
      return false;
    }
  }
  return true;
};
Run Code Online (Sandbox Code Playgroud)


小智 10

AbstractChosen.browser_is_supported 功能不允许您在移动设备和Internet Explorer上使用此插件,因此您可以自行破解.

找到以下行chosen.jquery.js并注释此代码.现在,所选插件将适用于移动设备.

if (!AbstractChosen.browser_is_supported()) {
    return this;
}   
if (!AbstractChosen.browser_is_supported()) {
    return;  
}
Run Code Online (Sandbox Code Playgroud)

  • 伟大的。成功了!多谢。 (2认同)
  • 非常感谢您的大力帮助。非常感谢。 (2认同)

小智 6

对我来说就是这一行:

        }, AbstractChosen.browser_is_supported = function() {
            return "Microsoft Internet Explorer" === window.navigator.appName ? document.documentMode >= 8 : /iP(od|hone)/i.test(window.navigator.userAgent) ? !1 : /Android/i.test(window.navigator.userAgent) && /Mobile/i.test(window.navigator.userAgent) ? !1 : !0
        }
Run Code Online (Sandbox Code Playgroud)

改成这样,它就像一个魅力。

}, AbstractChosen.browser_is_supported = function() {          
return true;
}
Run Code Online (Sandbox Code Playgroud)


小智 5

在平板电脑中禁用

 AbstractChosen.browser_is_supported = function () {
        if (window.navigator.appName === "Microsoft Internet Explorer") {
            return document.documentMode >= 8;
        }
        //if (/iP(od|hone)/i.test(window.navigator.userAgent))
        if ((/iPhone|iPod|iPad|Android|android|playbook|silk|BlackBerry/).test(navigator.userAgent)) 
        {
            return false;
        }
        if (/Android/i.test(window.navigator.userAgent)) {
            if (/Mobile/i.test(window.navigator.userAgent)) {
                return false;
            }
        }
        return true;
    };
Run Code Online (Sandbox Code Playgroud)


car*_*mat 5

在这里发布作为我已经实现的后备方案,因为我依赖 ChosenJS 插件来工作,以便可以应用自定义 CSS。希望这对其他人有帮助。

免责声明:鉴于问题,@dreamweiver 的上述答案仍应为可接受的答案。

var chosenSelects = $('.ui-select').find('.chosen-select, [chosen]'),
    $select, $option;

if (chosenSelects) {
    chosenSelects.chosen();

    // Check for 'chosen' elements on mobile devices
    // -----
    // Given that ChosenJS has expressly been disabled from running
    // on mobile browsers, the styles have to be applied manually.
    // Source: https://github.com/harvesthq/chosen/pull/1388
    // -----
    // The code below gathers all 'chosen' selectors and adds
    // 'chosen-mobile' as a className. This className is then
    // used to apply the necessary styles for mobile browsers.
    // Within each select, if an 'option' has an empty value,
    // then that value will be given 'selected' and 'disabled'
    // attributes to act as a placeholder, adopting the text
    // in the 'data-placeholder' as the text to be displayed.
    if ( /iP(od|hone)/i.test(window.navigator.userAgent)
        || (/Android/i.test(window.navigator.userAgent) && /Mobile/i.test(window.navigator.userAgent)) ) {
        chosenSelects.each(function () {
            $select = $(this);
            $select.addClass('chosen-mobile');

            $select.find('option').each(function () {
                $option = $(this);

                if ( $option.val() == '' ) {
                    $option
                        .attr('selected', 'selected')
                        .attr('disabled', 'disabled')
                        .text( $select.data('placeholder') );
                }
            });
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

有了这个,我就可以.ui-select .chosen-mobile应用必要的 CSS。