[英]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();
});
大家好,我剛剛開始學習jquery。 我有兩個選擇字段,分別包含各大洲和國家。
我的需求nascodere顯示兩個DIV,即“ lib-ass”和“ soc-first”:
1)當您選擇歐洲大陸時,將顯示DIV“ lib-ass”。 那沒問題
2)當您未選擇歐洲大陸DIV“ lib-ass”時,將隱藏。 那沒問題
3如果您未選擇國家DIV英國,則必須隱藏“ lib-ass”。 這不行 。
當您選擇國家DIV英國時,您必須顯示“ lib-ass”。 這樣不好
我的問題是,當您選擇國家ID為“ lib-ass”的英國DIV標簽時,我無法隱藏顯示。
我哪里錯了?
我希望盡可能多地提供信息。 謝謝
http://jsfiddle.net/J2XDk/1/這是您要執行的操作嗎?
我做了一些更改:可以通過直接在select上調用.val()
來檢索select
值(無需查找所選選項)。 我也專門使用選擇框ID來查找它,因為僅執行$('select')
是模棱兩可的。 將值保存到變量,這樣您就不必多次執行jQuery。 歐洲支票的替代條件可以使用其他條件,無需專門檢查!= "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();
});
您可以在邏輯中添加以下內容:
$("#country").change(function(){
if($("#country option:selected").val() == "Britain") {
$('#lib-ass').show();
$('#soc-pri').show();
}else{
$('#lib-ass').hide();
}
}).change();
國家/地區是您所選國家/地區的選擇框。 井號(#)用於調用Jquery中的特定ID。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.