简体   繁体   English

未在函数中定义的Javascript变量

[英]Javascript variable not defining in function

I'm writing a small script to clear my search forms when they're clicked on for "only" the first time. 第一次单击“仅”时,我正在编写一个小的脚本来清除搜索表单。 So far I have the follow: 到目前为止,我有以下几点:

$(function() {
  if(typeof firstClick == 'undefined') 
  { 
    $("#search").live("focus", function() {
      $(this).val("");
      var firstClick = true;
    });
  }
});

However, even with defining the variable with var firstClick = true; 但是,即使使用var firstClick = true定义变量; in the function, the script seems to pass the if statement every time. 在函数中,脚本似乎每次都传递if语句。 I'm sure I'm missing something silly, but I can't seem to figure it out. 我确定我缺少一些愚蠢的东西,但是我似乎无法弄清楚。

I've tried defining the var firstClick outside of the function as a false boolean, and then checking to see if it's false or not but I still can't seem to get the variable to go true in the function. 我尝试将函数外部的var firstClick定义为一个假布尔值,然后检查是否为假,但是我似乎仍然无法使变量在函数中变为真。

var firstClick = true;

You are redeclaring firstClick here. 您正在重新声明firstClick这里。 Remove var , and define it outside of both functions. 删除var ,然后在两个函数之外定义它。 Adding var before an assignment tells Javascript to make a variable scoped to the current function, which in your case is anonymous function assigned to the focus event. 在赋值之前添加var告诉Javascript定义一个范围为当前函数的变量,在您的情况下,该变量是分配给焦点事件的匿名函数。 The outer function can't see it since you limited its scope with var . 外部函数无法看到它,因为您使用var限制了它的范围。

Variables exist only in the scope in which they are defined (using var ) and in descendant scopes. 变量仅存在于定义它们的作用域(使用var )和其后继作用域中。 Your typeof call is in an ancestor scope. 您的typeof调用在祖先范围内。 firstClick does not exist there. firstClick在那里不存在。 You should define the variable as false in the outer scope, then redefine it as true inside. 您应该在外部范围中将变量定义为false ,然后在内部将其重新定义为true Furthermore, the conditional should be inside the click handler. 此外,条件应该点击处理程序中。

$(function() {
  var firstClick = false;

  $("#search").live("focus", function() {
    if (!firstClick) {
      $(this).val("");
      firstClick = true; // change the value in the outside scope
    }
  });
});

Your firstClick var is defined within the local scope of the callback function, thus is not visible to the parent code. 您的firstClick是在回调函数的本地范围内定义的,因此对父代码不可见。

var firstClick = false;

$(function() {
  if(!firstClick) 
  { 
    $("#search").live("focus", function() {
      $(this).val("");
      firstClick = true;
    });
  }
});

The problem is one of scope. 问题是范围之一。 The variable firstClick exists only within the focus event. 变量firstClick仅存在于焦点事件中。 You need to declare firstClick as a global within the namespace, like so: 您需要将firstClick声明为名称空间内的全局变量,如下所示:

(function($) {
   var firstClick = false;

   $('#search').live('focus', function() {
        if (typeof firstClick === 'undefined') {
           $(this).val('');
           firstClick = true;
        }
   });
}(jQuery));

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

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