jquery更改不适用于select

use*_*883 5 html jquery select

为什么这不起作用?我不确定我做错了什么.我确定jquery正在页面上工作.

谁能帮我?提前致谢

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" type="text/css" href="styles/derweco.css"/>
    <script type="text/javascript" src="scripts/jquery-2.1.0.js"></script>
    <script type="text/javascript">

        $('#numbers').change(function(){
            alert('other value');
        });
    </script>
</head>
<body>
<div id="home">
    <select id="numbers">
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
    </select>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

Fel*_*lix 6

您需要将代码包装在DOM就绪处理程序$(document).ready(function() {...});或更短的表单中$(function() {...});,以确保在执行jQuery代码之前已正确加载所有DOM元素.

$(function() {
    $('#numbers').change(function(){
        alert('other value');
    });
});
Run Code Online (Sandbox Code Playgroud)

小提琴演示