Dis*_*oat 2 forms ajax jquery submit
我正在尝试通过AJAX提交表单,而不是使用常规的POST提交。HTML只是一个标准格式,method="post"我的jQuery代码如下:
jQuery.noConflict();
jQuery(document).ready( function($) {
var $form = $('#default_contact');
$form.submit( function() {
$.ajax({
type: 'POST',
url: $form.attr( 'action' ),
data: $form.serialize(),
success: function( response ) {
console.log( response );
}
});
return false;
});
});
Run Code Online (Sandbox Code Playgroud)
(基于此答案)
我从Submit函数返回false,但是表单仍在提交,我不知道为什么。没有收到任何Firebug错误。
如果html设置正确,则您的代码可以正常工作。这是我的测试html(使用php),因此您可以将其与自己的进行比较。
<?php
if (!empty($_POST)) {
die("<pre>" . print_r($_POST, true) . "</pre>");
}
?>
<html>
<head>
<title>ajax submit test</title>
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<script type="text/javascript">
jQuery.noConflict();
jQuery(document).ready(function ($) {
var $form = $('#default_contact');
$form.submit(function () {
$.ajax({
type: 'POST',
url: $form.attr('action'),
data: $form.serialize(),
success: function (response) {
alert(response);
}
});
return false;
});
});
</script>
</head>
<body>
<form id="default_contact" action="formsubmit.php" method="post">
<input type="hidden" value="123" name="in1" />
<input type="hidden" value="456" name="in2" />
<input type="submit" value="send" />
</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
22630 次 |
| 最近记录: |