javascript - 使用 AJAX 提交表单 PHP JQuery 将我带到操作页面

标签 javascript php jquery ajax forms

我试图在不刷新页面的情况下提交此表单,但是当我提交时它会将我带到操作页面。我的代码有什么问题? 这是我的表格:

<form class="ajax" action="/../addchannel/createUser.php" method="post" >
<input type="text" name="userName" placeholder="userName"><br>
<input type="text" name="email" placeholder="email"><br>
<input  type="submit" value="submit" >
</form>';

这是我的脚本:

$('form.ajax').on('submit', function () {
    var that = $(this),
        url = that.attr('action'),
        type = 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) {
        consol.log(response)
    }
});
});

最佳答案

将事件作为参数传递并使用 event.preventDefault()

示例

$('form.ajax').on('submit', function (e) {
      e.preventDefault();

关于javascript - 使用 AJAX 提交表单 PHP JQuery 将我带到操作页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19564281/

相关文章:

javascript - 你能从屏蔽的 URL 中获取参数吗?

php - 使用文件存储库时无法使Composer加载类

php - CakePHP - 如何更新多条记录

javascript - 每个都在数组的追加中

javascript - 如何监控 contenteditable div 中的文本样式和格式?

javascript - 在 javascript 中使用 webkit-playsinline

Javascript 重定向和成功调用函数

javascript - 使用 clear 函数重新启用 radio 输入

javascript - 如何在jrunscript或jjs中访问stdin(在win7上)

PHP选择查询未获取更新记录