2016-03-27 53 views
0

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

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(); 
      } 

     }); 
    }); 

谢谢!

+0

我没有尝试使用.blur(),但事情是我试图表现/只有当第一个选择框被更改时隐藏特定选项,所以不确定它会有帮助。 – tlt2w

+0

你试过.on('change',function(){}); ?我已经删除了第一个评论,因为它没有这个问题的上下文..我也发现这个帖子,也许它帮助[jQuery .change()事件不在IE中触发](http://stackoverflow.com/questions/9921498/jquery-change-event-not-firing-in-ie) – rmjoia

+0

我尝试了'jQuery(“#selecbox-dependency”)on(“change”,“select”,function(){'和'jQuery(“ .container“)。on(”change“,”#selectbox-dependency“,function(){,'但不幸的是我无法让它工作。 – tlt2w

回答

0

这可能是移动设备上val()的问题。看看这个答案是让你排序:

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

的核心问题是隐藏(.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); 
     } 

    }); 
}); 
相关问题