如何将 JQuery 事件处理程序分配给特定事件
How to assign a JQuery event handlers to a specific event
我有以下名为Howto的Javascript函数,它在按下按钮时执行:
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 事件处理程序中的任何代码。
然而,正如名称"全局"所暗示的那样,它们也是由其他Jquery AJAX调用发起的。不幸的是,这是不可取的,因为我在脚本中有其他 AJAX 调用。我需要仅在执行$('#elementtoupdate').load('serverside/file_path.php')
时执行代码。
最好的方法是什么?
load
本身具有非全局回调:
.load( url [, data ] [, complete(responseText, textStatus, XMLHttpRequest) ] )
因此:
$('#elementtoupdate').load('serverside/file_path.php', function() {
// complete
});
你应该接受David Hedlund的回答。为了完整起见:
您可以更改:
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(){
//code to be executed before send
$('#elementtoupdate').load('serverside/file_path.php', function(){
//code to be executed after completed
})
}
相关文章:
- jQuery在页面加载时分配点击事件处理程序
- 在插件中分配事件
- Node.js,为事件分配回调函数'数据'
- 分配和删除单击事件
- 查找将 onclick 事件分配给节点的代码
- 通过参数将传递的函数分配给动态创建的输入类型的onclick事件
- 在分配事件时避免代码重复
- 无法分配点击事件
- 内联事件处理程序与基于脚本的处理程序分配
- 将事件处理程序分配给元素集合
- jQuery:如何从一个元素捕获事件并将其分配给另一个元素
- 在循环内分配点击事件
- 如何将委托事件分配给特定的 Dom 元素
- 使用不正确的迭代器值进行事件分配
- 为什么HTML按钮没有't调用onclick事件分配的函数
- 将onmouseover事件分配给动态生成列表
- 如何将基于enter键的事件分配给选定的<
- 内的元素
- 同时将onclick事件分配给多个DOM元素
- Javascript事件冲突,但不确定如何识别第一个事件分配
- 将JQuery事件分配给自定义对象方法