我想让这个脚本工作:
<script type="text/javascript">
$(document).ready(function () {
$("#councilsel").change(function () {
var val = $(this).val();
if (val == "manlysel") {
$("#suburbsel").html("<option value='test'>item1: test 1</option><option value='test2'>item1: test 2</option>");
} else if (val == "warringahsel") {
$("#suburbsel").html("<option value='Beacon Hill'>Beacon Hill</option><option value='Brookvale'>Brookvale</option><option value='Collaroy'>Collaroy</option><option value='Collaroy Plateau'>Collaroy Plateau</option><option value='Cottage Point'>Cottage Point</option><option value='Cromer'>Cromer</option><option value='Curl Curl'>Curl Curl</option><option value='Dee Why'>Dee Why</option><option value='Freshwater'>Freshwater</option><option value='Ingleside'>Ingleside</option><option value='Manly Vale'>Manly Vale</option><option value='Narrabeen'>Narrabeen</option><option value='Narraweena'>Narraweena</option><option value='North Balgowlah'>North Balgowlah</option><option value='North Curl Curl'>North Curl Curl</option><option value='North Manly'>North Manly</option><option value='Queenscliff'>Queenscliff</option><option value='Wheeler Heights'>Wheeler Heights</option>");
} else if (val == "pittwatersel") {
$("#suburbsel").html("<option value='test'>item3: test 1</option><option value='test2'>item3: test 2</option>");
}
});
});
</script>
使用此html表单
<select name="councilsel" id="councilsel">
<option value="selopt" selected="selected">--select an option</option>
<option value="manlysel">Manly Council</option>
<option value="warringahsel">Warringah Council</option>
<option value="pittwatersel">Pittwater Council</option>
</select>
<select name="suburbsel" size="1" id="suburbsel">
<option value="">--select a option--</option>
</select>
但是当我去查看它时它会继续产生这个错误
<script type="text/javascript">
$(document).ready(function () {
Uncaught ReferenceError: $ is not defined
$("#councilsel").change(function () {
var val = $(this).val();
if (val == "manlysel") {
$("#suburbsel").html("<option value='test'>item1: test 1</option><option value='test2'>item1: test 2</option>");
} else if (val == "warringahsel") {
$("#suburbsel").html("<option value='Beacon Hill'>Beacon Hill</option><option value='Brookvale'>Brookvale</option><option value='Collaroy'>Collaroy</option><option value='Collaroy Plateau'>Collaroy Plateau</option><option value='Cottage Point'>Cottage Point</option><option value='Cromer'>Cromer</option><option value='Curl Curl'>Curl Curl</option><option value='Dee Why'>Dee Why</option><option value='Freshwater'>Freshwater</option><option value='Ingleside'>Ingleside</option><option value='Manly Vale'>Manly Vale</option><option value='Narrabeen'>Narrabeen</option><option value='Narraweena'>Narraweena</option><option value='North Balgowlah'>North Balgowlah</option><option value='North Curl Curl'>North Curl Curl</option><option value='North Manly'>North Manly</option><option value='Queenscliff'>Queenscliff</option><option value='Wheeler Heights'>Wheeler Heights</option>");
} else if (val == "pittwatersel") {
$("#suburbsel").html("<option value='test'>item3: test 1</option><option value='test2'>item3: test 2</option>");
}
});
});
</script>
这是应该做的:
http://jsfiddle.net/HAkkF/embedded/result/
这是该页面的链接:
答案 0 :(得分:6)
您忘记在页面中添加jQuery
。
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>