Jquery on() 在单个元素上加载事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17400152/
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 on() load event on a single element
提问by FLX
I'm sorry if this question has already been asked, but i didn't find a solution.
如果已经有人问过这个问题,我很抱歉,但我没有找到解决方案。
I have 3 radios elements and I want to check the value when the selection changes and when the page loads.
我有 3 个无线电元素,我想在选择更改和页面加载时检查值。
I would do that by using only the on() function.
我会只使用 on() 函数来做到这一点。
My problem is that only the change event is triggered.
我的问题是只触发了更改事件。
Here is my current code :
这是我当前的代码:
$('.user').on('load change', function(){
if($(this).val() == 'client'){
$('#user_parent').removeAttr('disabled').closest('tr').show(200);
}else{
$('#user_parent').attr('disabled', 'disabled').closest('tr').hide(200);
}
});"
I also tried to replace load by ready, but it failed too. What the problem ? Isn't the load event available for a single element ?
我也尝试通过 ready 替换 load,但它也失败了。什么问题?load 事件不是可用于单个元素吗?
The code is placed in $(document).ready(...), and the elements are all displayed when the page is sent.
代码放在$(document).ready(...)中,页面发送时元素全部显示。
Thanks
谢谢
回答by Vogel612
the load
event will be called the moment all child elements of the listened element are loaded. in your case this might be before the ready event is called, thus rendering your handler to load (which is appended after document.ready) useless.
该load
事件将在侦听元素的所有子元素加载时被调用。在您的情况下,这可能是在调用 ready 事件之前,从而使您的处理程序加载(附加在 document.ready 之后)无用。
for reference see JQuery apiwhere you will find the following:
作为参考,请参阅JQuery api,您将在其中找到以下内容:
The load event is sent to an element when it and all sub-elements have been completely loaded. This event can be sent to any element associated with a URL: images, scripts, frames, iframes, and the window object.
当一个元素和所有子元素都被完全加载时,加载事件被发送到一个元素。此事件可以发送到与 URL 关联的任何元素:图像、脚本、框架、iframe 和 window 对象。
this also means you need an URL, so you can listen to the load event. as you have not provided further code I assume you do indeed have an URL you can listen to.
这也意味着您需要一个 URL,以便您可以侦听 load 事件。由于您没有提供进一步的代码,我假设您确实有一个可以收听的 URL。
This might be the most probable cause though. if you do not have any URL associatedwith (at least one) child element(s) there will be no load eventyou can listen to.
不过,这可能是最可能的原因。如果您没有与(至少一个)子元素关联的任何 URL,则您将无法收听加载事件。
try this instead:
试试这个:
$(document).ready(function(){
checkUserVal();
$('.user').on('change', checkUserVal);
});
var checkUserVal = function(){
//do the check
if($('.user').val() == 'client'){
$('#user_parent').removeAttr('disabled').closest('tr').show(200);
}else{
$('#user_parent').attr('disabled', 'disabled').closest('tr').hide(200);
}
};
i made the code a method for improved readability ;)
我使代码成为一种提高可读性的方法;)
回答by Vezquex
As Vogel612 explained, load
doesn't fire for most elements.
正如Vogel612 解释的那样,load
对于大多数元素都不会触发。
ready
is only for document
.
ready
仅用于document
.
You can use each
to run your event handler initially.
您可以使用each
最初运行您的事件处理程序。
$(document).ready(function(){
$('.user')
.each(user_handler)
.on('change', user_handler);
});
var user_handler = function(){
// this
};