简体   繁体   English

多个显示/隐藏jQuery

[英]Multiple show / hide jquery

My code below is showing the proper city/state div's when the matching country is selected from the drop down, but it's not hiding the previous one if the user decides to select another state. 我的以下代码显示了从下拉列表中选择匹配的国家/地区时正确的城市/州的div,但是如果用户决定选择其他州,则不会隐藏前一个城市/州的div。 Do I have to create an if/else each for each div i want to hide? 我是否需要为要隐藏的每个div创建一个if / else?

<script type="text/javascript">
$(document).ready(function() {
...
//city & state display 

   $("#ctry").change(function() {
      $(".state").hide();

      var stateSelect = $("#state_" + $(this).val());

      if (stateSelect.length === 0)
         $("#state_label").hide(); 
      else {
         $("#state_label").show();
         stateSelect.show();
      }       
   });     
});
</script>

html code: html代码:

    <label>Country/Locale:</label>
        <select id="ctry" name="country">
        <option value="">-- Select</option>
        <option value="US">United States</option>
        <option value="CA">Canada</option>
        <option value="GB">United Kingdom</option>        
        <option value="AU">Australia</option>       
        </select><br /> 

<!-- US -->        
    <div id="state_US" style="display:none">
    <label id="state_label" style="display:none">State:</label>
    <span class="rstate">
        <select name="u_state">
        <option value="">-- State US</option>
        <option value="AL">Alabama</option>
        </select>
    </span>
    &nbsp;&nbsp;Zip or City:<input style="margin-left:43px;" type="text" name="locus" id="locus" size="30" />
    </div>

<!-- CA -->   
    <div id="state_CA" style="display:none">
    <label id="state_label" style="display:none">Province:</label>
    <span class="rstate">
        <select name="c_state">
        <option value="">-- Prov CA</option>
        <option value="ON">Windsor</option>
        </select>
    </span>
    &nbsp;&nbsp;Postal or Place:<input style="margin-left:43px;" type="text" name="locca" id="locca" size="30" />
    </div>

<!-- GB -->
    <div id="state_GB" style="display:none">
        <label id="state_label" style="display:none">City or Region:</label>
        <span class="rstate">
            <select name="k_state">
            <option value="">-- Place</option>
            <option value="AU">UK!</option>
            </select>
        </span>
    </div>

<!-- AU -->
    <div id="state_AU" style="display:none">
        <label id="state_label" style="display:none">City or Region:</label>
        <span class="rstate">
            <select name="a_state">
            <option value="">-- Place</option>
            <option value="AU">UK!</option>
            </select>
        </span>
    </div>
  1. You cannot have multiple elements with the same ID. 您不能有多个具有相同ID的元素。 Your label state_Label is not unique. 您的标签state_Label不是唯一的。 You should change this, otherwise some browsers will not show your label. 您应该更改此设置,否则某些浏览器将不会显示您的标签。
  2. You hide all elements having the css-class "state". 您隐藏所有具有css类“状态”的元素。 But there is no element having this class. 但是没有元素具有此类。 I guess that you need to add class="state" to all of your state_* -divs. 我想您需要将class="state"添加到所有state_* -divs中。

Something like this logic might work for you: 像这样的逻辑可能对您有用:

var previous;
$(document).ready(function() {    
    $("#ctry").change(function() {
        $(".state").hide();

        var stateSelect = $("#state_" + $(this).val());

        if (stateSelect.length === 0) {
            $("#state_label").hide(); 
        } else {
            if (previous) {
                previous.hide(function() {
                    $("#state_label").show();
                    stateSelect.show();
                    previous = stateSelect;
                });
            } else {
                $("#state_label").show();
                stateSelect.show();
                previous = stateSelect;
            }
        }       
   });     
});

Though it would be easier if they all had the same CSS class, with which you could easily hide them and then show the one that was selected. 虽然如果它们都具有相同的CSS类会更容易,您可以使用它们轻松地隐藏它们, 然后显示所选的那个。

Remove the state_label ids.. 删除state_label ID。
Add the unused class state to the state_CA , state_US etc. elements.. 将未使用的类state添加到state_CAstate_US等元素。

So each state group should be 所以每个州组应该

<div id="state_US" class="state" style="display:none">
<label style="display:none">State:</label>
<span class="rstate">
    <select name="u_state">
    <option value="">-- State US</option>
    <option value="AL">Alabama</option>
    </select>
</span>
&nbsp;&nbsp;Zip or City:<input style="margin-left:43px;" type="text" name="locus" id="locus" size="30" />
</div>

And change your script to be 并将您的脚本更改为

<script type="text/javascript">
$(document).ready(function() {
...
//city & state display 

   $("#ctry").change(function() {
      $(".state").hide();

      var stateSelect = $("#state_" + $(this).val());
      stateSelect.show();      
   });     
});
</script>

Demo at http://jsfiddle.net/gaby/PYx2v/ 演示在 http://jsfiddle.net/gaby/PYx2v/

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

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