简体   繁体   English

jQuery-使用select标签显示和隐藏DIV

[英]jquery - show and hide DIV with the select tag

<label for="continent">Select Continent</label>
<select id="continent" onchange="countryChange(this );">
    <option value="empty">Select a Continent</option>
    <option value="North America">North America</option>
    <option value="South America">South America</option>
    <option value="Asia">Asia</option>
    <option value="Europe">Europe</option>
    <option value="Africa">Africa</option>
</select>
<br/>
<label for="country">Select a country</label>
<select id="country">
    <option value="0">Select a country</option>
</select>
<div id="soc-pri">
    <label for="company">Company</label>
    <input name="customer" type="radio" value="company" />
    <label for="private">Private</label>
    <input name="customer" type="radio" value="private" />
</div>
<div id="lib-ass">
    <label for="individual firm / freelancer">Individual Firm / Freelancer Professionista</label>
    <input name="customer" type="radio" value="privato" />
    <label for="association">Associazione</label>
    <input name="customer" type="radio" value="association" />
</div>




$(document).ready(function () {
    $("select").change(function () {
        if ($("select option:selected").val() == "Europe") {
            $('#lib-ass').show();
            $('#soc-pri').show();

        } else if ($("select option:selected").val() != "Europe") {
            $('#lib-ass').hide();
        }
    }).change();
});

Hello everyone, I just started to study jquery. 大家好,我刚刚开始学习jquery。 I have two select fields that contain the continents and countries. 我有两个选择字段,分别包含各大洲和国家。

My needs nascodere show the two-DIV, "lib-ass" and "soc-first": 我的需求nascodere显示两个DIV,即“ lib-ass”和“ soc-first”:

1) When you select the continent of Europe the DIV "lib-ass," appears. 1)当您选择欧洲大陆时,将显示DIV“ lib-ass”。 That is OK 那没问题

2) When you do not select the continent Europe DIV "lib-ass", hides. 2)当您未选择欧洲大陆DIV“ lib-ass”时,将隐藏。 That is OK 那没问题

3 When you do not select the country DIV Britain the "lib-ass" must hide. 3如果您未选择国家DIV英国,则必须隐藏“ lib-ass”。 This is not OK . 这不行

When you select the country DIV Britain the "lib-ass" you have to show. 当您选择国家DIV英国时,您必须显示“ lib-ass”。 This is not OK 这样不好

My problem is that I can not hide-show, when you select your country Britain the DIV tag with id "lib-ass." 我的问题是,当您选择国家ID为“ lib-ass”的英国DIV标签时,我无法隐藏显示。

Where am I wrong? 我哪里错了?

I hope averdato much information as possible. 我希望尽可能多地提供信息。 thanks 谢谢

http://jsfiddle.net/carmy/jg7Ls/6/ http://jsfiddle.net/carmy/jg7Ls/6/

http://jsfiddle.net/J2XDk/1/ Is this what you're looking to do? http://jsfiddle.net/J2XDk/1/这是您要执行的操作吗?

I made a few changes: select values can be retrieved by calling .val() directly on the select (no need for finding the selected option). 我做了一些更改:可以通过直接在select上调用.val()来检索select值(无需查找所选选项)。 I also look it up specifically using the select box ID since just doing $('select') is ambiguous. 我也专门使用选择框ID来查找它,因为仅执行$('select')是模棱两可的。 Save values to variables so you don't have to execute the jQuery multiple times. 将值保存到变量,这样您就不必多次执行jQuery。 The alternate condition for the Europe check can just use else, no need to specifically check for != "Europe" 欧洲支票的替代条件可以使用其他条件,无需专门检查!= "Europe"

$(document).ready(function () {

    $("select").change(function () {
        var continent = $('#continent').val(),
            country = $('#country').val(),
            $libass = $('#lib-ass'),
            $socpri = $('#soc-pri');

        if (continent === "Europe") {
            $libass.show();
            $socpri.show();

        } else {
            $libass.hide();
        }

        if(country === 'Britain') {
            $libass.hide();                
        } else {
            $libass.show();   
        }

    }).change();
});

You could add the following to your logic: 您可以在逻辑中添加以下内容:

$("#country").change(function(){
            if($("#country option:selected").val() == "Britain") {
                $('#lib-ass').show(); 
                $('#soc-pri').show();
            }else{
                $('#lib-ass').hide();
            }
 }).change();

Where country is the select box of your selected country. 国家/地区是您所选国家/地区的选择框。 The hashtag (#) is used to call a specific ID in Jquery. 井号(#)用于调用Jquery中的特定ID。

Here is a working example 这是一个有效的例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM