2011-02-25 83 views
1

我一直在阅读很多关于jQuery和显示div的主题,但是我还没有找到我的具体问题的答案。这里的东西:jQuery show div if value> x

根据我的价值,我想显示div A或div B.选择字段填充20个国家,其中19个获得相同的div(B),只有一个get的另一个div(A )。得到的div A的值为“value = 1”,所以我想应用“if select value> 1,show div B”的原则。但是,我无法实现它的工作。我的另一个select-show-div机制是基于确切的价值(我已经在下面发布),但是这个if-else事情让我发疯。

任何帮助,将不胜感激!

我的旧值=确切的代码:

$('.div').hide(); 
$('#country').change(function() { 
    $('.div').hide(); 
    $('#country' + $(this).val()).show(); 
    }); 
}); 

以及相应的HTML:

<div id="country1" class="div"> 
BLABLA 
</div> 

<div id="country2" class="div"> 
BLABLA 
</div> 

etc 

回答

2

如果你有一个选择菜单,或div的我不明白。一个可能的解决方案如下:

$('select').change(function() { 
    var countryVal = $("select option:selected").val(); //get the value of the selected 
    $("#country1, #country2").hide(); //hide country divs, of previous selection 
    if(countryVal == 1) {$("#country1").show();} else {$("#country2").show()}; //if countryVal is 1 (the diffrent value) show the div #country1 else show the div #country2 
}); 

更新演示:http://jsfiddle.net/YnYxD/1

+0

很大的一块代码索蒂里斯的,基本上解决了我的问题!我有一个选择,这反过来触发div的。我更新了小提琴以展示我的使用方式,但是您的代码非常棒。 [更新jsfiddle](http://jsfiddle.net/YnYxD/4/) – 2011-02-25 19:28:07

+0

@Jaap欢迎:) – Sotiris 2011-02-25 19:30:52

1

如果我理解正确,所有其他国家都值> 1,所以你不能只需使用该值来生成id。如果值为1,则ID必须为1,否则为2。

尝试:

$('#country').change(function() { 
    $('.div').hide(); 
    var id = $(this).val() == 1 ? 1 : 2; 
    $('#country' + id).show(); 
    }); 
});