我正在尝试了解有关 jquery 自定义事件的更多信息,因此我制作了这个简单的代码来进行测试:
<!DOCTYPE html>
<html>
<head>
<title>Jquery custom events</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
$(function(){
$('#link1').click(function(){
var that = $(this);
$.ajax({
url: "http://localhost/jquery/index.html",
beforeSend: function(event){ // if returned false, stops ajax request
return that.trigger('ajax:beforesend',[{p1: '1'},{p2: '2'}]);
},
success: function(){
that.trigger('ajax:success');
},
error: function(){
}
});
return false;
});
$('#link1').bind('ajax:beforesend',function(e,data,data2){
alert("success"+data2.p2);
return false;
});
$('#link1').bind('ajax:success',function(e){
alert("success");
});
});
</script>
</head>
<body>
<a id="link1">link1</a>
<div id="box" style="width:500px;height:150px;background-color: gray;margin-top: 50px;">
result
</div>
</body>
</html>
我的问题是:在 beforesend 函数中,如果触发的自定义事件的结果为假但不起作用,我想中止 ajax 请求,我错过了什么?
编辑:请不要专注于 beforesend 功能。我真正想要的是如何将自定义事件结果(例如真或假)返回到调用事件的位置(在这种情况下是在发送函数之前)。正如我所说,这只是样板代码。不是现实世界的项目。