我試圖在選擇雙重條件下顯示/隱藏某些單選按鈕。不知道是否可以完成。選擇-jquery在雙重條件下顯示/隱藏
我已經這第一個選擇填充第二個選擇。第二個選擇確實是幾個選擇顯示和隱藏取決於第一個選擇的選擇。
目標是根據選擇的選項顯示/隱藏某些單選按鈕。所以如果你有select1 = A和select2 = a'顯示某個單選按鈕。
到目前爲止,我管理這個成功地與一個單一選擇的條件(JS):
$(".chosen").chosen();
$("#a").chosen().change(function() {
var value = $(this).val();
if (value=="Opt1") {
$("#b").show();
} else {$("#b").hide();}
}).trigger('change');
這是顯示/隱藏第二選擇的功能。
正如我已經嘗試了很多東西,並沒有成功,我到了一個絕望的解決方案,它是忘了雙重條件,並在第二個觸發單選按鈕選擇的選擇,像這樣(JS):
$(".chosen").chosen();
$("#c").chosen().change(function() {
var value = $(this).val();
if (value=="Opt1") {
$("#radio").show();
} else {$("#radio").hide();}
}).trigger('change');
然而它毀了整個事情,因爲它顯示這個「c」選擇應該隱藏時根據以前的功能。任何想法如何克服這一點?
UPDATE
這是整個代碼。我刪除了id與實名無關:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Chosen: A jQuery Plugin by Harvest to Tame Unwieldy Select Boxes</title>
<link rel="stylesheet" href="docsupport/style.css">
<link rel="stylesheet" href="docsupport/prism.css">
<link rel="stylesheet" href="chosen.css">
<style type="text/css" media="all">
/* fix rtl for demo */
.chosen-rtl .chosen-drop { left: -9000px; }
</style>
</head>
<body>
<form>
City
<div id="city">
<select data-placeholder="Select city" id="a" class="chosen-select" style="width:350px;" tabindex="2">
<option value="London">London</option>
<option value="Paris">Paris</option>
</select>
</div>
<br>
Trial
<div id="trial1">
<select data-placeholder="Select court" class="chosen-select" style="width:350px;" tabindex="2">
<option value="Court of District"> Court of District </option>
<option value="Magistrate’s Court"> Magistrate’s Court </option>
</select>
</div>
<div id="trial2">
<select data-placeholder="Select court2" class="chosen-select" style="width:350px;" tabindex="2">
<option value="Cour de cassation"> Cour de cassation </option>
<option value="Cour d’apell"> Cour d’apell </option>
</select>
</div>
<br>
<!--- this is a hidden radio that should show up when city== 「Paris」 and trial2== 「Cour d’apell」 --->
<div id=radio1><br>
<input type="radio" name="radiob" value="Apell Criminal"> Apell Criminal <br>
<input type="radio" name="radiob" value="Apell Civil" checked> Apell Civil <br>
</div>
<br>
<input type="button" id="btncalc" value="Go on">
</form>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script>
<script src="chosen.jquery.js" type="text/javascript"></script>
<script src="docsupport/prism.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
var config = {
'.chosen-select' : {},
'.chosen-select-deselect' : {allow_single_deselect:true},
'.chosen-select-no-single' : {disable_search_threshold:10},
'.chosen-select-no-results': {no_results_text:'Oops, nothing found!'},
'.chosen-select-width' : {width:"95%"}
}
for (var selector in config) {
$(selector).chosen(config[selector]);
}
<!--- Hides the third select and radio buttons--->
$('#trial2').hide();
$('#radio1').hide();
<!--- Shows/hides second and third select, depending on first select’s choice--->
$(".chosen").chosen();
$("#city").chosen().change(function() {
var value = $(this).val();
if (value=="London") {
$("#trial1").show();
$("#trial2").hide();
} else if (value == "Paris") {
$("#trial1").hide();
$("#trial2").show();
}
}).trigger('change');
<!--- show/hide the radio button--->
$('.chosen').chosen().change(onChange);
var onChange = function() {
var a = $('#city').find('select').val();
var b = $('#trial1').find('select').val();
var c = $('#trial2').find('select').val();
/* do all conditional checks here on values a, b, and c */
/* here is an example check on the values of a and b: */
if (a === 'Paris' && c === ‘Cour d’apell’) {
/* show radios */
$("#radio1").show();
}
}; $('.chosen').chosen().change(onChange);
</script>
</body>
</html>
提供HTML將會有幫助。 – 76484
謝謝。 JSFiddle:http://jsfiddle.net/7VFQQ/ – chancar
你似乎在將'chosen()應用到'div'元素。我認爲它只能應用於'select'元素。 – 76484