Javascript 如何将JQuery事件处理程序分配给特定事件
我有一个名为Howto的Javascript函数,它在按下按钮时执行:Javascript 如何将JQuery事件处理程序分配给特定事件,javascript,jquery,ajax,Javascript,Jquery,Ajax,我有一个名为Howto的Javascript函数,它在按下按钮时执行: function howto(){ $('#elementtoupdate').ajaxSend(function() { //Code to execute }); $('#elementtoupdate').ajaxComplete(function() { //Code to execute }); $('#elementtoupdate').load('
function howto(){
$('#elementtoupdate').ajaxSend(function() {
//Code to execute
});
$('#elementtoupdate').ajaxComplete(function() {
//Code to execute
});
$('#elementtoupdate').load('serverside/file_path.php')
}
该函数使用JQuery分配两个JQuery全局Ajax事件处理程序,它们在任何Ajax调用启动时启动。然后该函数继续并调用JQuery.load
函数,全局Ajax事件处理程序中的任何代码都会被启动
但是,顾名思义,Global这个词也由其他jqueryajax调用启动。不幸的是,这是不可取的,因为脚本中还有其他AJAX调用。我只需要在执行$('#elementtoupdate').load('serverside/file_path.php')
时执行代码
这样做的最佳方式是什么 本身具有非全局回调:
.load( url [, data ] [, complete(responseText, textStatus, XMLHttpRequest) ] )
因此:
其本身具有非全局的回调:
.load( url [, data ] [, complete(responseText, textStatus, XMLHttpRequest) ] )
因此:
你应该接受大卫·赫德隆德的回答。为了完整起见: 您可以更改:
function howto(){
$('#elementtoupdate').ajaxSend(function() {
//code to be executed before send
});
$('#elementtoupdate').ajaxComplete(function() {
//code to be executed after completed
});
$('#elementtoupdate').load('serverside/file_path.php')
}
致:
你应该接受大卫·赫德隆德的回答。为了完整起见: 您可以更改:
function howto(){
$('#elementtoupdate').ajaxSend(function() {
//code to be executed before send
});
$('#elementtoupdate').ajaxComplete(function() {
//code to be executed after completed
});
$('#elementtoupdate').load('serverside/file_path.php')
}
致:
但不适用于ajaxSend。为了支持它,提问者必须直接调用
$.ajax()
,并使用在发送之前选项。@FrédéricHamidi:这是真的。不过,对于OP来说,在howto
函数中运行ajaxSend
代码就足够了,因为它们至少在时间上是相关的。但对于ajaxSend
,就不足够了。为了支持它,提问者必须直接调用$.ajax()
,并使用在发送之前选项。@FrédéricHamidi:这是真的。不过,对于OP来说,在howto
函数中运行ajaxSend
代码就足够了,因为它们至少是暂时相关的。Ok。评论撤回…:)好的。撤回评论…:)