jQuery:在另一个 jquery 操作后刷新 div?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1488162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery: Refresh div after another jquery action?
提问by Marko
How do I refresh element (div, class whatever) after another jquery action?
如何在另一个 jquery 操作之后刷新元素(div,类等)?
I have this action which is deleting a record from database and I need to refresh div which is fetching some other data from database as well...
我有一个从数据库中删除记录的操作,我需要刷新 div,它也从数据库中获取一些其他数据......
Well here's code:
那么这里的代码:
$(function() {
$(".delete").click(function() {
var commentContainer = $(this).parent();
var id = $(this).attr("id");
var string = 'id='+ id ;
$.ajax({
type: "POST",
url: "delete.php",
data: string,
cache: false,
success: function(){
commentContainer.slideUp('slow', function() {$(this).remove();});
}
});
return false;
});
});
Thanks for all help! :)
感谢所有帮助!:)
回答by Vincent Ramdhanie
During an AJAX call you can use the success function to run commands after a successful call. You already have a function in your code so its a simple matter of updating that function. The second div that you will like to update can be loaded with AJAX too.
在 AJAX 调用期间,您可以使用成功函数在成功调用后运行命令。您的代码中已经有一个函数,因此更新该函数很简单。您要更新的第二个 div 也可以使用 AJAX 加载。
$.ajax({
//other options
success:function(){
commentContainer.slideUp('slow', function() {$(this).remove();});
$('#otherdiv').load('urlofpagewith info.php');
}
});
回答by karim79
Simply expose the data
parameter of the success callback and replace the contents of whichever element, in this example div id="someDiv":
只需公开data
成功回调的参数并替换任何元素的内容,在本例中为 div id="someDiv":
$.ajax({
type: "POST",
url: "delete.php",
data: string,
cache: false,
success: function(data){
commentContainer.slideUp('slow', function() {$(this).remove();});
$('#someDiv').html(data);
}
});
See ajax optionsfor more information.
有关更多信息,请参阅ajax 选项。
Also, for the love of god don't use type names as variable names, it's really scary and terrible practice, even though string
is not a reserved word in Javascript(use data: str
or data: dataString
instead).
此外,出于上帝的爱,不要使用类型名称作为变量名称,这确实是可怕和可怕的做法,即使string
它不是Javascript 中的保留字(使用data: str
或data: dataString
替代)。
回答by NawaMan
Why don't you add a function that do the refreshing after the first ajax is success (assuming that you cannot combine to the two into one ajax requist which a much more efficient).
为什么不添加一个在第一个 ajax 成功后进行刷新的函数(假设你不能将两者结合成一个更有效的 ajax 请求)。
...
function doRefresh_ofAnotherDiv() {
$.ajax({
type: ...,
url: ...,
data: ...,
cache: ...,
success: function(){
// updateAnotherDiv();
}
});
}
...
$.ajax({
type: "POST",
url: "delete.php",
data: string,
cache: false,
success: function(){
commentContainer.slideUp('slow', function() {$(this).remove();});
doRefresh_ofAnotherDiv();
}
});
...
Hope this helps.
希望这可以帮助。
回答by helloandre
almost all jquery functions have the ability to use callback functions. these are called whenever the original action is finished executing. so this can be used with any function, not just ajax.
几乎所有的jquery 函数都有使用回调函数的能力。只要原始操作完成执行,就会调用它们。所以这可以用于任何函数,而不仅仅是 ajax。