简体   繁体   English

初学者Javascript混淆-控制台表格

[英]Beginners Javascript Confusion - Forms To Console

I'm learning Javascript & have set up the following test form, which I'm trying to input to the console when the submit button is pressed. 我正在学习Javascript,并设置了以下测试表单,当按下“提交”按钮时,我试图将其输入到控制台。 I'm not having much luck, can anyone tell me the issue? 我运气不好,有人可以告诉我这个问题吗?

html: HTML:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <link rel="stylesheet" type="text/css" href="css/style.css">
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script src="https://maps.googleapis.com/maps/api/js"></script>
    <script src="js/search.js"></script>
  </head>
  <body>
    <form id="search">
      <p><input id="search-name" type="text"></p>
      <p><input id="search-submit" type="submit"></p>
    </form>
  </body>
</html>

js: js:

$("#search").submit(function(event){
    event.preventDefault();
    var name = ("#search-name").val();

    console.log("Search Term Was: "+name);  
});

Try this line instead. 试试这一行。 It looks like you're missing jQuery's $ 看来您缺少jQuery的$

var name = $("#search-name").val();` <-- Need the `$` or this line will fail (you should see errors in the console).

Full Code 完整代码

$("#search").submit(function(event){
    event.preventDefault();
    var name = ("#search-name").val();

    console.log("Search Term Was: "+name);  
});

http://learn.jquery.com/using-jquery-core/ http://learn.jquery.com/using-jquery-core/

在console.log之后添加以下语句

return false;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM