I have the following excerpt of code on my website (also available as a jsFiddle ). It's 2 select fields that show different data depending on selection.
I have 2 bugs .
1) When I select US
& then Alaska
, the Alaska( ak
) div does not display as I'd hope.
&
2) When I move from Alaska
back to Choose a state...
it displays everything again (United Kingdom & US), instead of resorting back to just the US( us
) div. Help?
CODE:
<form method="get" action="/" id="languageSwitch">
<fieldset>
<select name='cat' id='cat' class='postform' >
<option value='0' selected='selected'>Choose a country…</option>
<option class="level-0" value="united-kingdom">United Kingdom</option>
<option class="level-0" value="us">US</option>
</select>
<script type="text/javascript">
jQuery('#cat').change(function(){
jQuery('#cat').change(function() {
jQuery("#statecat").toggle(jQuery(this).val() == "us");
});
if(jQuery('#cat').val()=="0")
jQuery('form#languageSwitch').siblings('div').show();
else{
jQuery('form').siblings('div').hide();
jQuery('.'+jQuery('#cat').val()).show();
}
});
</script>
</fieldset>
</form>
<form method="get" action="/" id="stateSwitch">
<fieldset>
<select name='statecat' id='statecat' class='postform' >
<option value='0' selected='selected'>Choose a state…</option>
<option class="level-0" value="ak">Alaska</option>
<option class="level-0" value="wy">Wyoming</option>
</select>
<script type="text/javascript">
jQuery('#statecat').change(function(){
if(jQuery('#statecat').val()=="0")
jQuery('form#stateSwitch').siblings('div').show();
else{
jQuery('form').siblings('div').hide();
jQuery('.'+jQuery('#statecat').val()).show();
}
});
</script>
</fieldset>
</form>
<div class="united-kingdom">
<h2>United Kingdom</h2>
<ul></ul>
</div>
<div class="us">
<h2>US</h2>
<div class="ak">
<h2>Alaska</h2><ul>
<li class="animal-listing" id="post-123">
<a href="http://localhost:8888/test/wordpress/company/kiwi-kompany/">Alaska Test</a><br />
Address:<br />Address<br />
Country: Alaska<br />
URL: http://www.somesite.co.nz<br />
Telephone: 01902<br />
Fax: 01293
</li>
<h2>Employees</h2>
<span>Me</span><br /><span></span><br /><span>(012)020-0202</span><br /><span>email@site.com</span><br /><img alt='' src='' class='avatar avatar-128 photo' height='128' width='128' /></ul></div>
<div class="as">
<h2>American Samoa</h2>
<ul></ul>
</div>
<div class="wy">
<h2>Wyoming</h2>
<ul></ul>
</div>
</ul></div>
Concerning the first bug, I suggest you give a class ( state1
) to each state; this makes things easier.
You can then try this:
<script type="text/javascript">
$('#statecat').change(function(){
$('.state1').each(function(){$(this).hide(); });
if($('#statecat').val()=="ak")
$('.ak').show();
});
</script>
<div class="uk">
<h2>United Kingdom</h2>
<ul></ul>
</div>
<div class="us">
<h2>US</h2>
<div class="ak state1">
<h2>Alaska</h2><ul>
...
</div>
<div class="as state1">
<h2>American Samoa</h2>
<ul></ul>
</div>
<div class="wy state1">
<h2>Wyoming</h2>
<ul></ul>
</div>
</div>
As Rhumborl pointed it, there are many problems in your code. Be careful to the fact that HTML code with incorrectly nested and/or closed tags will very probably give you trouble , even if they are displayed correctly in the browser.
For this reason, before writing any CSS and Javascript code, you should validate your HTML code in an HTML validator like: http://validator.nu/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.