我正在尝试做一个简单的ajax表单发布。我认为$ .ajax行中的代码有问题。一旦代码到达$ .ajax部分,console.log()将无法正常工作,并且该表单仅按常规重定向到ajax-report-comment.php页面,而不是通过ajax。到目前为止,我的console.log函数报告数据,并且不重定向到页面。
有人看到我在做什么错吗?从本质上讲,我希望它能成功地向用户发出警报。
提前致谢!
码:
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('form.ajax').on('submit',function() {
var that = $(this),
url = that.attr('action'),
method = that.attr('method'),
data = {};
that.find('[name]').each(function(index, value) {
var that = $(this),
name = that.attr('name'),
value = that.val();
data[name] = value;
});
$.ajax({
url: url,
type: type,
data: data,
success: function(response) {
console.log(response);
}
});
return false;
});
});
</script>
</head>
<body>
<form method="post" action="ajax-report-comment.php" class="ajax">
<input type="submit" value="Report" />
<input class="field" type="hidden" name="report_user_id" id="report_user_id" value="5"/>
<input class="field" type="hidden" name="report_comment_id" id="report_comment_id" value="33543"/>
</form>
</body>
</html>
参考方案
我很确定在现代版本的jQuery中已取消取消默认操作的return false;
(如果使用migrate
插件,则会看到有关该警告的警告),因此应使用:
$('form.ajax').on('submit',function(e) {
e.preventDefault();
// the rest of your code
PHP Count数组元素 - php嗨,有人可以解释为什么这会返回“数组由0个元素组成”。 :$arr = array(1,3,5); $count = count($arr); if ($count = 0) { echo "An array is empty."; } else { echo "An array has $count elements.…
PHP:从函数返回值并直接回显它? - php这可能是一个愚蠢的问题,但是……的PHPfunction get_info() { $something = "test"; return $something; } html<div class="test"><?php echo get_info(); ?></div> 有没有办…
PHP:将数据从二维数组复制到一维数组的最快方法 - php我有一个巨大的二维PHP数组,带有500万行。$t = [ [ "id" => 1, "name" => "foo" ], [ "id" => 2, "name" => "bar" ] ]; 现在,我必须将此数组的I…
php-printf和sprintf具有不同的输出 - php我编写了以下微型php程序来测试printf和sprintf:<?php $str_1 = printf("%x%x%x", 65, 127, 245); $str_2 = sprintf("%x%x%x", 65, 127, 245); echo $str_1 . "\n"; echo $s…
PHP-正则表达式删除引号并添加大括号? - php好吧,我不愿承认这一点,但是我对REGEX感到很困难,我永远找不到关于如何设置表达式的不错的教程。所以说我有这样的事情context['something'] 我想将所有事件更改为context[something] 那我有' . $var . ' 我想将所有事件更改为{var} 这是当前的概念,但是我在正则表达式部分…