gpt4 book ai didi

javascript - jQuery 不发布 - (jQuery - ColdFusion)

转载 作者:行者123 更新时间:2023-11-29 17:13:01 24 4
gpt4 key购买 nike

我有一个小函数可以使用 ColdFusion 9 和 jQuery 从我的数据库中删除记录

该功能存在于其他 3 个位置,它与预期的功能相同且正常运行,但此页面似乎有错误。

HTML 表单代码

<form name="devRatingCat" method="post" action="" >
<table class="table table-bordered table-striped" >
<tr>
<th>&nbsp;</th>
<th>ID</th>
<th>Category Name</th>
</tr>
<cfloop query="categories">
<tr>
<td><input class="checkbox" type="checkbox" name="mark" value="#recID#"></td>
<td>#recID#</td>
<td>#categoryname#</td>
</tr>
</cfloop>
</table>
<hr />
<div class="pull-left">
<button class="btn btn-danger" type="button" onClick="dlteCatR(mark);" >Delete</button>
</form>

jQuery

function dlteCatR(field)
{
var $srt = $(field);
var r = confirm("Are you sure you want to delete this Category? \n You will not be able to revert this change!")
if(r==true){
for(i=0; i<$srt.length; i++){
if($srt[i].checked == true){
var url="surveyAdmin.cfc?wsdl&method=deleteRateCat&recId="+$srt[i].value;
$.post(url);
}
}
window.location.reload();
}else{
return false;
}
}

surveyAdmin.cfc 方法

<cffunction name="deleteRateCat" access="remote" returntype="void" output="no"  hint="Delete Rating Categories.">
<cfargument name="recID" type="string" required="true" hint="The id of the rating category to delete.">
<cfquery datasource="#dsn#">
delete from rating_categories
where id = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.recID#">
</cfquery>
</cffunction>

我正在使用 firebug 来跟踪调用,但它没有给我任何关于为什么它不起作用的很好的解释。

此外,当我在 firebug 中复制链接并在浏览器中自行运行时,交易会正常进行

最佳答案

$.post() 发送异步请求。如果您在该请求完成之前重新加载页面,该请求将被中止。

在您的例子中,您在 for 循环中一次发送 n 个请求,然后立即重新加载页面(使用此行 window.location.reload();)他们有时间完成。要解决这个问题,您可以将它们全部合并到一个 $.post 请求中并使用成功回调,或者您可以存储从 $.post() 返回的每个 promise 对象在一个数组中并将其传递给 $.when

我建议使用第一种解决方案,将所有请求合并为一个请求并使用成功回调,但是这将需要您修改当前的 cfc 方法以接受要一次删除的多个记录,或者创建一个新的可以处理它的cfc方法。

一种方法是让您的方法能够处理 ID 列表而不是单个 ID。

<cffunction name="deleteRateCat" access="remote" returntype="void" output="no"  hint="Delete Rating Categories.">
<cfargument name="recID" type="string" required="true" hint="The id of the rating category to delete.">
<cfquery datasource="#dsn#">
delete from rating_categories
where id in (<cfqueryparam cfsqltype="cf_sql_integer" value="#ListAppend(arguments.recID, 0)#" list="yes">)
</cfquery>
</cffunction>

以及与之配套的 js:

function dlteCatR(field){               
var r = confirm("Are you sure you want to delete this Category? \n You will not be able to revert this change!")
if(r==true){
var recIdList = $("[name=" + field + "]:checked").map(function(){
return this.value;
}).get().join(",");
var url="surveyAdmin.cfc?wsdl&method=deleteRateCat&recId="+recIdList;
$.post(url).done(function(){
window.location.reload();
});
}
}

关于javascript - jQuery 不发布 - (jQuery - ColdFusion),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20408500/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com