0

I am very new to jQuery. I am trying to create a simple page. Using google's hosted jQuery. Based on many examples from stackoverflow, I have written this small piece of code. The change event never gets fired. I also do not see 'can not load' alert. If I look in the firebug and expand the script tag from google it shows it as 'undefined'. I am testing locally, not using any web-server. Can anyone please point out my mistakes? Code as below:

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link href="./styles/qForm.css" rel="stylesheet" type="text/css"/>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
    <script>
        if (!window.jQuery) {
            alert("can not load');
        }

        $('#choice').change(function () {
                    alert('here!');
                    if($(this).val() == '0') $(this).addClass('empty');
                    else $(this).removeClass('empty')
                });

        $('#choice').change();

    </script>

</head>
<body>
    <div id="surround">
        <div id="top">
            <h3 id="title"> Enter Information </h3>
        </div>
        <div id="bottom">
             <select id="choice">
                 <option style="color: gray;" value="0" selected="selected">Select...</option>
                 <option value="1">one</option>
                 <option value="2">two</option>
                 <option value="3">three</option>
             </select>
        </div>
    </div>
</body>
4

4 回答 4

2

alert您在函数的引号上有错字。

alert("can not load');

引号必须相同。这应该是您的代码不起作用的第一个原因。

alert("can not load");
于 2013-10-11T18:21:20.863 回答
0

将 http: 添加到您的脚本源

 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
于 2013-10-11T18:20:53.390 回答
0

使用 jQuery,通常将其包装在“就绪”函数中。

<script>
$(document).ready(function(){
    $('#choice').change(function () {
        alert('here!');
    });
});
</script>

这意味着 jQuery 将等到页面完全加载后再应用绑定。如果它尝试将绑定应用于尚不存在的元素(页面尚未呈现这些元素),它将被忽略。

于 2013-10-11T18:20:59.270 回答
0

使用本地文件时,您的<script src="//ajax...."浏览器无法理解。将此更改为<script src="http://ajax...."

于 2013-10-11T18:21:03.917 回答