0

我正在尝试在 jQuery 中触发提交处理程序,但在提交时它会进入操作页面。我究竟做错了什么?

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
<script>
$('#target').submit(function() {
    alert('Handler for .submit() called.');
    return false;
});
</script>
<form id="target" action="destination.html">
    <input type="text" value="Hello there" />
    <input type="submit" value="Go" />
</form>
<div id="other">
    Trigger the handler
</div>
</body>
</html>
4

2 回答 2

1

尝试:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
<script>
$(function() {
  $('#target').submit(function() {
      alert('Handler for .submit() called.');
      return false;
  });
});
</script>
<form id="target" action="destination.html">
    <input type="text" value="Hello there" />
    <input type="submit" value="Go" />
</form>
<div id="other">
    Trigger the handler
</div>
</body>
</html>
于 2013-04-01T17:27:42.473 回答
0

两个更改...一,将您的代码包装在一个onready中,二,添加一个preventDefault

$(function() {
  $('#target').submit(function(event) {
      event.preventDefault();
      alert('Handler for .submit() called.');
      return false;
  });
});

更多信息:http ://api.jquery.com/event.preventDefault/

于 2013-04-01T17:29:27.667 回答