我正在学习Javascript&设置了以下测试表单,我在按下提交按钮时尝试输入到控制台。我运气不好,有谁能告诉我这个问题?
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:
$("#search").submit(function(event){
event.preventDefault();
var name = ("#search-name").val();
console.log("Search Term Was: "+name);
});
答案 0 :(得分:3)
请尝试使用此行。看起来你错过了jQuery的$
var name = $("#search-name").val();` <-- Need the `$` or this line will fail (you should see errors in the console).
完整代码
$("#search").submit(function(event){
event.preventDefault();
var name = ("#search-name").val();
console.log("Search Term Was: "+name);
});
答案 1 :(得分:0)
在console.log
之后添加以下语句return false;