2016-03-27 56 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); 
     } 

    }); 
}); 
相關問題