Py学习  »  Jquery

jquery keyup未引发警报

Madison Brann • 5 年前 • 1856 次点击  

我正在测试 keyup() 通过在文本字段中键入时发送警报,但不会发生任何情况。不知道我是否需要做单独的括号,或者如果我没有包括一个库。

    <html>
        <head>
            <title>City Finder</title>
            <script 
             src="http://code.jquery.com/jquery-3.2.1.min.js">
             
              
             $(document).ready(function()
             {
                  $( "#cityField" ).keyup(function()
                  {
                      window.alert("Handler for .keyup() called.");
                  });
             });
            </script>
        </head>
        <body>
            <form>
                Enter A Utah City: <input type = "text" id="cityField" value= ""><br>
                Suggestion: <span id="txtHint">Empty</span>
                <input id="weatherButton" type ="submit" value="Submit">
            </form>
            <p>City</p>
            <textarea id = "displayCity">No City</textarea>
            <p>Current Weather</p>
            <div id="weather">No Weather</div>
            <input type = "button" onclick = alert("hello"); name = "ok" value = "Climb out window">
           
    
    </html>
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/39166
 
1856 次点击  
文章 [ 2 ]  |  最新文章 5 年前
Bee
Reply   •   1 楼
Bee    6 年前

只需关闭第一个外部脚本包含并启动另一个为我工作的脚本标记。

<html>
        <head>
            <title>City Finder</title>
            <script src="http://code.jquery.com/jquery-3.2.1.min.js"></script>
           <script>
              $(document).ready(function(){
            
        
                  $("#cityField").keyup(function()
                  {
                      alert("This works");
                  });
             });
            </script>
        </head>
        <body>
            <form>
                Enter A Utah City: <input type = "text" id="cityField" value= ""><br>
                Suggestion: <span id="txtHint">Empty</span>
                <input id="weatherButton" type ="submit" value="Submit">
            </form>
            <p>City</p>
            <textarea id = "displayCity">No City</textarea>
            <p>Current Weather</p>
            <div id="weather">No Weather</div>
            <input type = "button" onclick = alert("hello"); name = "ok" value = "Climb out window">
           
    
    </html>
gavgrif
Reply   •   2 楼
gavgrif    6 年前

您遇到了一些语义问题(例如没有关闭body标记),还有一些拼写错误—我还将脚本块移动到了页面的末尾,以便可以在调用脚本之前加载页面。确保在将外部JS文件调用到页面时关闭脚本标记-它们需要位于自己的脚本标记中-而不是与页面内的javascript块连接。我还为文本区域添加了HTML5 doctype和占位符文本。您还应该为输入设置标签。

另外,调试时最好使用console.log()而不是alert。

<!doctype html>
 <head>
   <title>City Finder</title>
 </head>
 <body>
   <form>
    <label for ="cityField"> Enter A Utah City:</label>
     <input type = "text" id="cityField" value= ""><br>
      Suggestion: <span id="txtHint">Empty</span>
     <input id="weatherButton" type ="submit" value="Submit">
   </form>
   <label for="displayCity">City</label><br>
   <textarea id = "displayCity" placeholder="No city"></textarea>
   <p>Current Weather</p>
   <div id="weather">No Weather</div>
     <button type = "button" onclick = "alert('hello')" name = "ok" >Climb out window</button>
            
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
 $(document).ready(function() {
    $( "#cityField" ).keyup(function() {
        console.log("Handler for .keyup() called.");
    });
  });
</script>
</body>
</html>