简体   繁体   English

jQuery onload事件维护“这个”

[英]jQuery onload event maintaining “this”

 $(this).load(function(){
  if ($(this).val()!="") {
    $(this).next().css('display','none');
  }
});

I am trying to make a function that hides the element after it if it's value wasn't none, but when the load event is launched, the "this" keyword now points at "Document" !! 我正在尝试创建一个隐藏元素的函数,如果它的值不是none,但是当启动load事件时,“this”关键字现在指向“Document”!! i tried load/ready/bind load the control doesn't has an ID , what am i missing ...? 我试过加载/ ready / bind加载控件没有ID,我错过了什么......?

You can always preserve the value of this : 您可以随时保存的值this

var it = this;
$(this).load(function() {
  if ($(it).val() != '') ...

Why it might be that you'd assign a "load" handler to some sort of form field element is not clear, however. 但是,为什么你可能会为某种表单字段元素分配“加载”处理程序并不清楚。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM