我對編程非常陌生,並希望能夠得到一些有關此問題的幫助。我的網頁上有六個項目的下拉列表。我想打開與每個下拉選項關聯的唯一對話框。我不想在打開頁面時顯示任何對話框,但保持隱藏狀態,直到用戶從下拉菜單中選擇一個選項。我給每個對話框一個「隱藏答案」的類,以便在頁面打開時隱藏它們。我擁有的是在頁面刷新時顯示對話框,而不是在選擇下拉項時。我正在使用jQuery UI。如何根據下拉選擇啓動jQuery對話框
<script>
$(document).ready(function() {
$(".hide-answer").dialog({
autoOpen: false
});
var selPermit = document.getElementById("permit");
if (selPermit.selectedIndex === 1) {
$("#answer-1").dialog('open');
}
else if (selPermit.selectedIndex === 2) {
$("#answer-2").dialog('open');
}
else if (selPermit.selectedIndex === 3) {
$("#answer-3").dialog('open');
}
else if (selPermit.selectedIndex === 4) {
$("#answer-4").dialog('open');
}
else if (selPermit.selectedIndex === 5) {
$("#answer-5").dialog('open');
}
else if (selPermit.selectedIndex === 6) {
$("#answer-6").dialog('open');
};
});
</script>
<div id="permitForm" class="grouped">
<h2>Do I Need A Permit?</h2>
<select name="types" id="permit">
<option selected="selected" value="">-- select type --</option>
<option value="First">First</option>
<option value="Second">Second</option>
<option value="Third">Third</option>
<option value="Fourth">Fourth</option>
<option value="Fifth">Fifth</option>
<option value="Sixth">Sixth</option>
</select>
</div>
<div class="hide-answer" id="answer-1" title="First Condition">
<p>This is the description of when a first condition is needed.</p>
</div>
<div class="hide-answer" id="answer-2" title="Second Condition">
<p>This is the description of when a second condition is needed.</p>
</div>
<div class="hide-answer" id="answer-3" title="Third Condition">
<p>This is the description of when a third condition is needed.</p>
</div>
<div class="hide-answer" id="answer-4" title="Fourth Condition">
<p>This is the description of when a fourth condition is needed.</p>
</div>
<div class="hide-answer" id="answer-5" title="Fifth Condition">
<p>This is the description of when a fifth condition is needed.</p>
</div>
<div class="hide-answer" id="answer-6" title="Sixth Condition">
<p>This is the description of when a sixth condition is needed.</p>
</div>
</div>
</div>
這裏是一個測試小提琴:https://jsfiddle.net/Twisty/pnd51hau/ – Twisty
歡迎來到堆棧溢出。首先,'.dialog()'只是JQuery UI的一個特性。你在測試中使用這個嗎? – Twisty