jQuery - $.ajax();转到提交时的下一页


jQuery - $.ajax(); goes to the next page on submit

如果用户没有阅读条款和条件,我有一个红色的按钮。如果用户单击"确认"按钮,它会将日期和时间发布到数据库中,然后返回绿色的禁用按钮。但是,当我提交时,它会直接进入下一页。任何人都可以告诉我我的代码出了什么问题吗?

标记:

<div class="acknowledge">
    <form method="post" name="blitzread" action="blitzread.php" id="submit">
        <input type="hidden" name="emp_id" value="<?php echo $emp_id; ?>">
        <input type="hidden" name="current_time" value="<?php echo $current_time; ?>">
        <button type="submit" name="Submit" class="btn btn-large btn-danger">Acknowledge</button>
    </form>
</div>

Javascript:

$('#submit').submit(function() {
    e.preventDefault();
    $.ajax({
        data: $(this).serialize(),
        type: $(this).attr('method'),
        url: $(this).attr('action'),
        success: function(response) {
            $('.acknowledge').html("<button class="btn btn-large btn-success disabled">Acknowledge</button>");
        }
    });
    return false;
});

问题是e是未定义的。

该函数的第一个参数是 jQuery 事件对象,您可以在其上调用 preventDefault() 。所以你的代码应该看起来像这样:

$('#submit').submit(function(e) {
    e.preventDefault();

你忘了把e论点放在你的function()

你的代码应该是这样的:

$('#submit').submit(function(e) {
    e.preventDefault();
    e.stopPropagation();
    $.ajax({
        data: $(this).serialize(),
        type: $(this).attr('method'),
        url: $(this).attr('action'),
        success: function(response) {
            $('.acknowledge').html('<button class="btn btn-large btn-success disabled">Acknowledge</button>');
        }
    });
    return false;
});

这是给你的小提琴:http://jsfiddle.net/zur4ik/WVssV/

你忘了得到e参数

$('#submit').submit(function(e) {
    e.preventDefault();
    $.ajax({
        data: $(this).serialize(),
        type: $(this).attr('method'),
        url: $(this).attr('action'),
        success: function(response) {
            $('.acknowledge').html("<button class="btn btn-large btn-success disabled">Acknowledge</button>");
        }
    });
    return false;
});