jQuery 捕获内联 onclick 并添加回调
我以前从未见过有人这样做,所以我想我应该把它扔掉。
我有一个遗留页面,每个单选按钮列表上都有一个内联 onclick 事件:
onclick="handleService(document.forms.ContactForm,5,16,5,0,0);"
此代码执行 AJAX 调用以获取其他 HTML,然后根据单击的单选按钮附加其他表单元素。这个handleService函数包含在一个远程Javascript文件中,我无法在本地编辑或复制该文件,因为它是动态生成的并且可以更改。
我想知道是否可以使用 jQuery(或其他方式)捕获此 onclick 事件,然后以某种方式向其添加回调函数。
每次单击单选按钮时,我试图使两个表格列具有相同的高度。添加附加表单元素会导致其中一列的大小增大/缩小。我尝试在 onclick 中调用 handleService 之后直接添加函数来修复高度,但在添加其他表单元素之前高度就被修复了,大概是因为 AJAX 调用在我调用修复高度之前尚未完成(因此需要回调)。
更新 这是我对 ajaxComplete 的调用(也是像 ajaxSucess 这样的全局函数):
$('.gq_steps').ajaxComplete(function(e, xhr, settings) {
alert('here');
if (settings.url == 'geoquote_services.php') {
$('#first_col').height($('#third_col').height());
}
});
handleService 没有使用 jQuery,但这应该不重要,对吧?无论如何,ajaxSucess/ajaxComplete 应该获取所有 ajax 请求。我什至尝试将其简化为以下代码,但仍然没有被调用。
$(document).ajaxComplete(function() {
console.log('here');
});
我把它放在 $(document).ready(...) 调用中。不应该吗?
I've never seen anyone do this before so I thought I'd throw it out there.
I have a legacy page that has an inline onclick event on each of a list of radio buttons:
onclick="handleService(document.forms.ContactForm,5,16,5,0,0);"
This code does an AJAX call to grab additional HTML which then appends additional form elements depending on which radio button is clicked. This handleService function is being included from a remote Javascript file that I can't edit nor copy locally since it is generated dynamically and can change.
I'm wondering if it is possible with jQuery (or otherwise) to capture this onclick event and then somehow add a callback function to it.
I'm trying to get two table columns to be the same height each time the radio button is clicked. Adding the additional form elements causes one of the columns to grow/shrink in size. I've tried just adding my function to fix the height directly after the call to handleService in the onclick but the height gets fixed before the additional form elements get added, presumably because the AJAX call hasn't finished before my call to fix the height (hence the need for a callback).
UPDATE
Here is my call to ajaxComplete (also a global function like ajaxSucess):
$('.gq_steps').ajaxComplete(function(e, xhr, settings) {
alert('here');
if (settings.url == 'geoquote_services.php') {
$('#first_col').height($('#third_col').height());
}
});
handleService is not using jQuery, but that shouldn't matter, right? ajaxSucess/ajaxComplete should grab all ajax requests regardless. I even tried dumbing it down all the way to the following code and it is still not getting called.
$(document).ajaxComplete(function() {
console.log('here');
});
I've got this sitting inside a $(document).ready(...) call. Should it not be?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来您可能对 jQuery 的
ajaxSuccess()
事件处理程序感兴趣。由于您希望代码在请求完成后运行,并且您似乎无法访问其他文件以在其中添加回调,因此您可以使用绑定侦听器的严厉方法到
ajaxSuccess
,然后运行你的高度修复。像这样的东西:
$('.your-columns').ajaxSuccess(函数(e,xhr,设置){
// 测试一下这是否是我们的函数
// 然后修复高度。
});
请注意,
ajaxSuccess
将在每个 Ajax 调用上运行,无论发生了什么,这是相当严厉的,您肯定需要进行测试以确保您正在使用正确的响应并需要使用您的代码。由于 HTML 正在回归,您始终可以测试$(xhr.responseText)
中是否存在某些元素或类似的内容。Sounds like you may be interested in jQuery's
ajaxSuccess()
event handler.Since you want your code to run after that request completes, and you don't seem to have access to that other file to add a callback there, you can use the heavy-handed approach of binding a listener to
ajaxSuccess
and then running your height fix.Something like this:
$('.your-columns').ajaxSuccess(function(e,xhr,settings){
// Test to see if this is our function
// then fix heights.
});
Note that
ajaxSuccess
will run on every Ajax call, regardless of what happened, this it is fairly heavy-handed and you will definitely need to test to make sure you are working with the right response and need to use your code. Since HTML is coming back, you could always test for the presence of certain elements in$(xhr.responseText)
or something along those lines.