我試圖隱藏和顯示不同的divs取決於哪些單選按鈕人點擊。試圖隱藏/顯示div上單選按鈕單擊
jquery加載正常,我試過在頁面加載時發出警報。
非常類似的代碼,選擇方法等在頁面上工作過在同一目錄與同包括在同一臺服務器上
輸入框代碼:
<label class="radio"><input type="radio" name="input_sandwich_choice" value="Panini">Panini</label>
<label class="radio"><input type="radio" name="input_sandwich_choice" value="Sandwich">Sandwich</label>
<label class="radio"><input type="radio" name="input_sandwich_choice" value="Baguette">Baguette</label>
的divs是:
<div id="div_bread_options" class="collapse span3"> <!--2.2-->
<h4>Which bread would you like?</h4>
<label class="radio"><input id="id_option_bread_white" type="radio" name="input_bread_choice" value="white">White</label>
<label class="radio"><input id="id_option_bread_brown" type="radio" name="input_bread_choice" value="brown">Brown</label>
<label class="radio"><input id="id_option_bread_granary" type="radio" name="input_bread_choice" value="granary">Granary</label>
<label class="radio"><input hidden id="id_option_bread_none" type="radio" name="input_bread_choice" value=""></label>
</div> <!--2.2-->
<div id="div_butter_options" class="collapse span3"> <!--2.3-->
<h4>Would you like butter?</h4>
<label class="radio"><input type="radio" id="id_option_butter_yes" name="input_butter_choice" value="Yes">Yes</label>
<label class="radio"><input type="radio" id="id_option_butter_no" name="input_butter_choice" value="No">No</label>
<label class="radio"><input hidden type="radio" id="id_option_butter_none" name="input_butter_choice" value=""></label>
</div> <!--2.3-->
最後,jquery是:
<script>
$("input[name=input_sandwich_choice]").click(function() {
var checkedValue = $("input[name='input_sandwich_choice']:checked").val();
console.log(checkedValue);
if (checkedValue == "Panini") {
$("#div_bread_options").collapse('hide');
$("#div_butter_options").collapse('show');
} else if (checkedValue == "Sandwich") {
$("#div_bread_options").collapse('show');
$("#div_butter_options").collapse('hide');
} else if (checkedValue == "Baguette") {
$("#div_bread_options").collapse('show');
$("#div_butter_options").collapse('hide');
} else {
alert("Oops.");
}
});
</script>
只是不能確定爲什麼這不起作用...任何幫助非常感謝!謝謝
'alert'並不意味着你已加載的jQuery ...什麼你進入控制檯? – jycr753
然後您將該腳本放在HTML之後,否則您需要一個DOM準備好的處理程序! – adeneo
試試這個,而不是:'if(window.jQuery){alert(「jquery loaded」);}' – jycr753