Jquery on()在单个元素上加载事件 [英] Jquery on() load event on a single element

查看:128
本文介绍了Jquery on()在单个元素上加载事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很抱歉,如果这个问题已经被问到,但是我没有找到解决方案。



我有3个收音机元素,我想检查



我将只使用on()函数来执行此操作。



我的问题是只有更改事件被触发。



这是我现在的代码:

  $ ').on('load change',function(){
if($(this).val()=='client'){
$('#user_parent')。removeAttr('禁用')最接近('tr')。show(200);
} else {
$('#user_parent')。attr('disabled','disabled' ').hide(200);
}
});

我也试图通过准备就绪来替换负载,但是它也失败了
有什么问题?单个元素的加载事件不是可用的?



代码放在$(document).ready(...)中,元素在页面发送时都会显示。



谢谢

解决方案

load 事件将被称为加载所有元素的所有子元素的时刻。在你的情况下,这可能是在调用ready事件之前,因此使你的处理程序加载(在document.ready之后附加)无用。



以供参考,请参阅 JQuery api ,您将在其中找到以下内容: / p>


当它和所有子元素已经被完全加载时,加载事件被发送到元素。该事件可以发送到与URL相关联的任何元素:图像,脚本,框架,iframe和窗口对象。


也意味着你需要一个URL,所以你可以听负载事件。因为你没有提供进一步的代码,我假设你确实有一个你可以听的URL。


这可能是最可能的原因, 。如果您没有任何与相关联的网址(至少有一个)子元素,则您将无法收听


改为:

  $(document) ready(function(){
checkUserVal();
$('。user')。on('change',checkUserVal);
});

var checkUserVal = function(){
//检查
if($('。user')。val()=='client'){
$('#user_parent')。removeAttr('disabled')。nearest('tr')。show(200);
} else {
$('#user_parent')。attr('disabled','disabled')最近的('tr')hide(200);
}
};

我使代码成为一种提高可读性的方法;)


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.

I would do that by using only the on() function.

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 ?

The code is placed in $(document).ready(...), and the elements are all displayed when the page is sent.

Thanks

解决方案

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.

for reference see JQuery api where you will find the following:

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.

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.

This might be the most probable cause though. if you do not have any URL associated with (at least one) child element(s) there will be no load event you can listen to.

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 ;)

这篇关于Jquery on()在单个元素上加载事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆