jQuery在一些Mobile/IE浏览器上选择更改

jQuery select change on some Mobile/IE browsers

本文关键字:浏览器 选择 IE Mobile jQuery      更新时间:2023-09-26

我正在尝试实现两个不同选择框之间的依赖关系。在使用以下jQuery代码时,大多数普通浏览器都可以完美工作(Chrome/FF),但IE/Edge/一些移动浏览器不会受到代码的影响。

    jQuery(document).ready(function($){
        $("#selectbox-dependency").change(function() {
           if ($(this).val() == "Banana") {     //value of selectbox #1
                $(".yellow").show();   //classes for each of the options in selectbox #2
                $(".red").hide();
                $(".black").hide();
                $(".gold").hide();
            }else if ($(this).val() == "cherry"){
                $(".yellow").hide();
                $(".red").show();
                $(".black").hide();
                $(".gold").hide();
            } 
        });
    }); 

谢谢!

移动设备上的val()可能有问题。看看这个答案是否能让你分类:

http://stackoverflow.com/questions/4709093/jquery-mobile-val-not-working

核心问题是隐藏(.hide())选项不适用于IE和许多移动浏览器。我最终使用了这个定制:(不是100%最优,但足够好,适用于所有浏览器)

jQuery(document).ready(function($){
    $("#selectbox-dependency").change(function() {
       if ($(this).val() == "Banana") {     //value of selectbox #1
            $(".yellow").prop('disabled', false);   //classes for each of the options in selectbox #2
            $(".red").prop('disabled', true);
            $(".black").prop('disabled', true);
            $(".gold").prop('disabled', true);
        }else if ($(this).val() == "cherry"){
            $(".yellow").prop('disabled', true);
            $(".red").prop('disabled', false);
            $(".black").prop('disabled', true);
            $(".gold").prop('disabled', true);
        } 
    });
});