簡體   English   中英

jQuery單擊事件不起作用

[英]jQuery click event not working

以下代碼在使用XAMPP的localhost上運行良好。 但它不適用於另一台服務器。

<!DOCTYPE html>
<html>
    <head>
        <script src="jquery-latest.js"></script>
    </head>
    <body>
        word: <input type="text" id="sub" />
        user: <input type="text" id="user" />

        <button type="button" id="btn">Click Me!</button>

        <script>
            $("#btn").click(function () {
                var word=$("#sub").val();
                var usr=$("#user").val();
                alert("hi");
            });
        </script>
    </body>
</html>

我有來自Chrome檢查元素的2個錯誤:

  • Uncaught SyntaxError: Unexpected end of input jquery-latest.js:5669

  • Uncaught ReferenceError: $ is not defined

檢查jquery-latest.js是與html文件相同的目錄。 否則代碼是正常的,也可以。 在腳本中添加類型。 嘗試這個

<script type="text/javascript" src="jquery-latest.js"></script>

您需要在$(document).ready()事件中連接單擊處理程序。

<!DOCTYPE html>
<html>
    <head>
        <script src="jquery-latest.js"></script>
        <script>
        $(document).ready(function() {
            $("#btn").click(function () {
                var word=$("#sub").val();
                var usr=$("#user").val();
                alert("hi");
            });
        });
        </script>
    </head>
    <body>
        word: <input type="text" id="sub" />
        user: <input type="text" id="user" />

        <button type="button" id="btn">Click Me!</button>
    </body>
</html>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM