1

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

<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)
    }
});
});
4

6 回答 6

4

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

例子

$('form.ajax').on('submit', function (e) {
      e.preventDefault();
于 2013-10-24T11:11:24.467 回答
2

您最终忘记return false了或阻止了默认值。

 $('form.ajax').on('submit', function (event) {
    event.preventDefault();
var that = $(this),
    url = that.attr('action'),
    type = that.attr('method'),
    data = {};
   ....
  //or return false;
    
});
于 2013-10-24T11:11:03.227 回答
1

尝试这个..

<form method='post' action="javascript:mail();" >
<input type="text" class="input-large" id="user_name" name="name">
<input type="text" class="input-large" id="email" name="name">
<button type="submit" class="btn btn-primary">Submit</a>
</form>

function mail()
{
var name = $("#user_name").val();
var email = $("#email").val();
$.ajax({
                type:       "POST",
                url:        "contact.php",
            data:        "name=" + name+"&customer_mail="+email,
                success:    function(html) {
            //do ur function
                }

            });


}
于 2013-10-24T11:15:42.577 回答
1

使用e.preventDefault()return false;阻止表单提交

$('form.ajax').on('submit', function (e) {//Pass the event argument here
    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)
    }
   });
 e.preventDefault(); 
 //OR
return false;
});
于 2013-10-24T11:11:42.037 回答
1

使用event.preventDefault()

$('form.ajax').on('submit', function (e) {
    e.preventDefault();
    //code here
});
于 2013-10-24T11:12:26.763 回答
1

使用 jQuery ajax 不会阻止浏览器跟随表单操作页面。为此,您应该通过简单的函数阻止浏览器执行此操作:e.preventDefault()

这是你的代码:

//Pass the event argument (e)
$('form.ajax').on('submit', function (e) { 
    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)
    }
});

//Prevent Browser to follow form action link:
e.preventDefault();

//you can use also
// return false;

});
于 2013-10-24T11:20:56.503 回答