2016-03-15 38 views
-3

此代碼在Opera工作,問題是IE和Android。 使用hiden() javascript。第一選擇使用價值馬自達(馬克)第二顯示我們'歌劇':只有所有型號MAZDA(很好)。 'IE':向我們展示所有模型和所有optgroup。也許是在JS問題?Javascript optgroup隱藏不工作IExlorer

$(document).ready(function() { 
    $('#model optgroup').hide();enter code here 
    $('#Marka').change(function() { 
    var text = $(this).val(); 
    $('#model optgroup').hide(); 
    $('#model').val(''); 
    $('#model optgroup[label="' + text + '"]').css({ 
     'display': 'block' 
    }); 


    }); 
}); 

function usun() { 
    document.getElementById("first").style.display = "none"; 

} 

https://jsfiddle.net/p051m5u6/

+1

哇2X「 - 」?也許我的帖子的答案?或者我的問題太難了。 –

+0

可能的問題重複:[在Internet Explorer中顯示/隱藏選擇optgroup選項](http://stackoverflow.com/questions/20628466/show-hide-select-optgroup-options-in-internet-explorer)和[如何隱藏optgroup /選項元素?](http://stackoverflow.com/questions/2731668/how-to-hide-optgroup-option-elements)。 OS似乎不是一個因素。 – Roberto

+0

你可以試着重述一下你的問題嗎?很難理解你的實際問題是什麼... – radoh

回答

0

有你的HTML(的jsfiddle)一些錯誤:請注意,爲不同的ID使用不同的值。不能存在兩個或更多具有相同ID的元素。

我建議你的是保存和刪除準備好後根據選擇追加正確的文件上的optgroups解決辦法:

var optGroup = {}; 
 
$(function() { 
 
    // remove from second list all optgroup and save them in a global variable 
 
    $('#model optgroup').each(function(index, element) { 
 
    optGroup[element.label] = {'optGroup': element}; 
 
    $('#model optgroup:eq(0)').remove(); 
 
    }); 
 

 
    $('#Marka').change(function(){ 
 
    // remove the attached optgroup 
 
    $('#model optgroup').remove(); 
 
    // search for the saved optgroup and attach it to the select 
 
    $(optGroup[this.value].optGroup).appendTo('#model'); 
 
    $('#model optgroup option:selected').removeAttr("selected"); 
 
    $('#first2').prop('selected', true); 
 
    }); 
 
});
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script> 
 

 
<select id="Marka" name="marka_auta"> 
 
    <option id="first1" >Marka:</option> 
 
    <optgroup label="Marka Auta:"> 
 
     <option value="audi" >Audi</option> 
 
     <option value="mazda" >Mazda</option> 
 
    </optgroup> 
 
</select> 
 
<select id="model" name="model_auta"> 
 
    <option id="first2" disabled selected>Model:</option> 
 
    <optgroup id="models1" label="mazda"> 
 
     <option>RX-8</option> 
 
     <option>323F</option> 
 
     <option>6</option> 
 
    </optgroup> 
 

 
    <optgroup id="models2" label="audi"> 
 
     <option>A6</option> 
 
     <option>R8</option> 
 
     <option>A4</option> 
 
    </optgroup> 
 
</select>

+0

非常好!做得好。謝謝。 –