我遇到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">×</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?");
放在record.remove();
之后,我可以看到警报并执行后台代码,但不是停留在同一页面,而是将其重定向到'包括/ 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()
不起作用
答案 0 :(得分: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();
};
});
答案 1 :(得分:0)
假设“deleteform”是页面上表单元素的ID,在其中您有一个“submit”类型的输入或按钮,我建议更改delentry按钮以使其具有“按钮”类型。
大多数浏览器会在单击该按钮时具有“提交”类型的输入或按钮的表单上执行POST / GET。
然后我会更新你的jQuery事件处理程序以选择按钮并指定onclick事件:
$('#delentry').click(function() {
...
});
如果您希望保持某种形式的优雅降级,可以将标记保留为提交类型按钮并在$(document).ready()上,将按钮类型更新为“button”。