0
我想使用jQuery腳本,但它不顯示,因爲它應該。我讓它工作within JSFiddle。然而它在我的頁面中不起作用:當選擇一個選項時,它根本不顯示任何內容。我究竟做錯了什麼?jQuery腳本不工作在HTML頁面
<html>
<head>
<meta charset="UTF-8">
<title>title</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="css/faqtest.css" rel="stylesheet">
</head>
<body>
<script type="text/javascript">
$('select').on('change',function() {
var answer = $(this).val();
$('.answers').hide();
$('#'+answer).show();
})
</script>
<select>
<option disabled selected>choose</option>
<option value="answer1">answer1</option>
<option value="answer2">answer2</option>
<option value="answer3">answer3</option>
</select>
<div class="answers" id="answer1">answer1</div>
<div class="answers" id="answer2">answer2</div>
<div class="answers" id="answer3">answer3</div>
</body>
</html>
在運行腳本時沒有創建選擇元素。 – Teemu
把你的代碼放在'$(document).ready()'裏面。 – Barmar