1

我对 jquery $.post 函数有疑问。我经常使用这个函数,除了一种情况外,相同的函数工作正常,我不明白为什么它在执行脚本后一直重定向我,这里是 js 代码:

var record;    

$('.delbtt').on('click', function(e){
        e.preventDefault();
        var deleteid = $(this).parent().parent().find('#id').text();
        record = $(this).parent().parent();
        $('#delrecord').empty().val(deleteid);
    });

$(document).on('submit', '#delteform', function() {
        var formData = $(this).serialize();
        $.post('includes/delete.php',formData,processData);
        function processData(data){
            record.remove();    
        };
    });

HTML:

<!-- Modal -->
<div class="modal fade" id="deleteFormModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <form id="deleteform" class="form-horizontal" role="form" action="includes/delete.php" method="POST"> <!-- -->
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">Delete record</h4>
            </div>
            <div class="modal-body">
                    <p>Are you sure you want to delete this person?</p>
            </div>
                <input type="hidden" id="delrecord" name="delrecord" value="" />
            <div class="modal-footer">
                <button type="button" class="btn btn-default empty" data-dismiss="modal">Cancel</button>
                <button id="delentry" type="submit" class="btn btn-danger">Delete Person</button>
            </div>
        </form>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

我知道该函数已成功执行,因为如果我放alert("Why is this not working?");了 after record.remove();,我可以看到警报并在后台执行代码,但它不会停留在同一页面上,而是将我重定向到“includes/delete.php”。我尝试禁用我在我的 JS 中拥有的所有其他 $.post 函数,我尝试使用$('#deleteform').submit(),我尝试将它放在我的主要$(document).ready(function() {});相同结果之外......总是相同的结果在函数完成后重定向我而不是停留在页面上。有人知道我为什么会出现这种行为吗?

php代码:

<?php
include('budgetprop/Initialize.php');
Database::GetInstance()->ConnectToServer($errors);
$record = $_POST['delrecord'];

# CONNECT TO DB
if(Database::GetInstance()->ConnectToServer($errors)){
    $insertSQL1 = "DELETE FROM salaries WHERE record_id = '".$record."' ";
    $stmt1 = sqlsrv_query(Database::GetInstance()->databaseConnection, $insertSQL1);
    # IF THERE IS AN ERROR
    if(!$stmt1){
                echo "Error, cannot delete record"; 
    }else{
        Database::GetInstance()->LastInsertId($stmt1);
    }
    # IF ALL QUERIES WERE SUCCSESSFUL, COMMIT THE TRANSACTION, OTHERWISE ROLLBACK
    if($stmt1 && !$errors){
        sqlsrv_commit(Database::GetInstance()->databaseConnection);
        # FREE THE STATEMENT
        Database::GetInstance()->FreeDBStatement($stmt1);
        echo "success";
        return true;
    }else{
        sqlsrv_rollback(Database::GetInstance()->databaseConnection);
        # FREE THE STATEMENT
        Database::GetInstance()->FreeDBStatement($stmt1);
        return false;
    }
    # NO CONNECTION WAS MADE
}else{
    return false;
};
?>

preventDefault()也不起作用

4

2 回答 2

4

听起来您的表单仍在提交中,因此重定向。你可以用preventDefault()...阻止它

$(document).on('submit', '#deleteform', function(e) {
    e.preventDefault();
    var formData = $(this).serialize();
    $.post('includes/delete.php',formData,processData);
    function processData(data){
        record.remove();    
    };
});
于 2013-10-10T15:12:10.427 回答
0

假设“deleteform”是您页面上表单元素的 ID,其中您有一个“提交”类型的输入或按​​钮,我建议将删除按钮更改为“按钮”类型。

大多数浏览器将在单击该按钮时对具有提交类型的输入或按​​钮的表单执行 POST/GET。

然后我会更新您的 jQuery 事件处理程序以选择按钮并指定一个 onclick 事件:

$('#delentry').click(function() {
    ...
});

如果您希望保持某种形式的优雅降级,您可以将标记保留为提交类型按钮并在 $(document).ready() 上,将按钮的类型更新为“按钮”。

于 2013-10-10T15:25:15.687 回答