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