1

这是HTML代码

<select id="language">
    <option value="en">english</option>
    <option value="fr">french</option>
    <option value="gr">german</option>
</select>






<div id="related_en_content">
    english text
</div>

<div id="related_fr_content">
    french text
</div>

<div id="related_gr_content">
    german text
</div>

每个 div 都与选择中的一个选项相关,我想div在更改选择选项时替换。

这是我尝试过的:

$(document).ready(
    function() {
        $("#language").change(
            function() {
                $("#" + this.value).show().siblings().hide();
            });
        $("#language").change();
    }
4

4 回答 4

3
<select id="language">
    <option value="en">english</option>
    <option value="fr">french</option>
    <option value="gr">german</option>
</select>


<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>


<div>
<div id="related_en_content">
    english text
</div>

<div id="related_fr_content">
    french text
</div>

<div id="related_gr_content">
    german text
</div>
</div>
<script>
$("#language").on("change", function() {
    id = "related_" + $(this).val() + "_content";
    $("#" + id).show().siblings().hide()
})
</script>
于 2012-04-12T21:55:52.897 回答
2
$('[id^="related"]').not(':first').hide();

$("#language").on('change', function() {
    $("#related_"+this.value+"_content").show().siblings('[id^="related"]').hide();
});

小提琴

于 2012-04-12T21:51:42.223 回答
2
<select id="language" onchange:"javascript:handle_language(this)">
    <option value="en">english</option>
    <option value="fr">french</option>
    <option value="gr">german</option>
</select>


<script>
  function handle_language(what){
       document.getElementById('related_en_content').style.visibility='hidden';
       document.getElementById('related_fr_content').style.visibility='hidden';
       document.getElementById('related_gr_content').style.visibility='hidden';
     if(what.value=="en"){
       document.getElementById('related_en_content').style.visibility='visible';
     }
     if(what.value=="fr"){
       document.getElementById('related_fr_content').style.visibility='visible';
     }
     if(what.value=="gr"){
       document.getElementById('related_gr_content').style.visibility='visible';
     }
  }
</script>



<div id="related_en_content">
    english text
</div>

<div id="related_fr_content">
    french text
</div>

<div id="related_gr_content">
    german text
</div>
于 2012-04-12T21:51:51.367 回答
2
$(document).ready(function(){  
    $("#language").change(function(e){ 
        $('[id^="related"]')
        .not($('#related_'+$(this).val()+'_content'))
        .hide();
        $('#related_'+$(this).val()+'_content').show()
    }); 
});​

例子

于 2012-04-12T21:58:34.863 回答