PreventDefault不阻止表单发送

use*_*790 5 html javascript jquery

我有一个非常简单的页面,其中有一个表单,我试图阻止使用jQuery发送,但代码令人惊讶地无法正常工作.有任何想法吗?

<!DOCTYPE html>
<html>
   <head>
        <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
        <script>

            $("#searchForm").submit(function(e){
               e.preventDefault();
         });

    </script>

</head>
<body>
    <form id="searchForm">
      <input name="q" placeholder="Go to a Website">
      <input type="submit" value="Search">
    </form>
    <pre id="response"></pre>
</body>
Run Code Online (Sandbox Code Playgroud)

mar*_*nas 5

您需要将代码包装起来,.ready()以便在DOM准备好时执行它.

$(document).ready(function() {
     $("#searchForm").submit(function(e){
         e.preventDefault();
     });
});
Run Code Online (Sandbox Code Playgroud)