【问题标题】:How do I declare variables for multiple functions如何为多个函数声明变量
【发布时间】:2010-12-05 09:24:35
【问题描述】:

如果我错了,请纠正我,但是是否可以在脚本标记之后声明一个变量,以便可以在整个脚本中使用它?我试过这个,我的功能就像它不存在一样。我做错了什么,或者这应该发生。如果它们是完全相同的东西,我不想不得不为每个函数重新声明所有变量。

对不起

<script>
  var na=document.getElementById('nr');
  var ea=document.getElementById('er');
  var em=document.subscribe.email;
  var fn=document.subscribe.fname;
  var ln=document.subscribe.lname;
  var eml=document.subscribe.email.value.length;
  var fnl=document.subscribe.fname.value.length;
  var lnl=document.subscribe.lname.value.length;
  var at=document.subscribe.email.value.indexOf("@");
  var per=document.subscribe.email.value.lastIndexOf("."); 

function validate_form() {
  if((fnl<1 || lnl<1) && !eml<1){
      alert("Please enter your first and last name.")
      if(fnl<1){fn.focus()}else{ln.focus()}
      }
  else if((fnl<1 || lnl<1) && eml<1){
      alert("Please fill in all fields.")
      if(fnl<1){fn.focus()}else{ln.focus()}
      }  
  else if(eml<1 || at<1 || per-at<2 || eml-per<2){
      alert("Please enter a valid email address")
      em.focus()
      }    
  else if (at>1 && per-at>2 && eml-per>2 && fnl>1 && lnl>1){return true}
  vfn(); vln(); vem();
 return false}

当变量在函数内部时它工作得非常好,但是当它们像这样时,什么都不会发生。

【问题讨论】:

  • 我们猜测没有代码,总是发布代码:)

标签: javascript function variables declare


【解决方案1】:

问题在于变量(特别是 eml、fnl、lnl)包含在声明它们时获得的值。 JS 不会在每次调用函数时重新计算字符串的长度或元素的值。

当您在函数内移动这些变量时,实际上每次调用函数时它们都会“重新计算”。

我要做的是将分配给 DOM 元素的 var 留在函数之外,但将获取 DOM 元素的值/长度的 var 移动到函数中。然后你可以引用包含 dom 元素的 vars。

例如(部分代码):

var na=document.getElementById('nr'),
    ea=document.getElementById('er'),
    em=document.subscribe.email,
    fn=document.subscribe.fname,
    ln=document.subscribe.lname;

function validate_form() { var eml=em.value.length, fnl=fn.value.length, lnl=ln.lname.value.length, at=em.value.indexOf("@"), per=em.value.lastIndexOf("."); // Rest of code.

【讨论】:

    【解决方案2】:

    我实际上想出了如何去做。我刚刚了解了全局变量,以及如何在函数中声明它们。所以我把所有的变量放回函数中,删除了上面的“var”,现在它可以正常工作了。

    function validate_form() {
       na=document.getElementById('nr');
       ea=document.getElementById('er');
       em=document.subscribe.email;
       fn=document.subscribe.fname;
       ln=document.subscribe.lname;
       eml=document.subscribe.email.value.length;
       fnl=document.subscribe.fname.value.length;
       lnl=document.subscribe.lname.value.length;
       at=document.subscribe.email.value.indexOf("@");
       per=document.subscribe.email.value.lastIndexOf("."); 
      if((fnl<1 || lnl<1) && !eml<1){
          alert("Please enter your first and last name.")
          if(fnl<1){fn.focus()}else{ln.focus()}
          }
      else if((fnl<1 || lnl<1) && eml<1){
          alert("Please fill in all fields.")
          if(fnl<1){fn.focus()}else{ln.focus()}
          }  
      else if(eml<1 || at<1 || per-at<2 || eml-per<2){
          alert("Please enter a valid email address")
          em.focus()
          }    
      else if (at>1 && per-at>2 && eml-per>2 && fnl>1 && lnl>1){return true}
      vfn(); vln(); vem();
     return false}
    

    【讨论】:

      【解决方案3】:

      如果您将所有这些代码都包含在 window.onload 事件中会更好。或者在 jquery 的情况下在 $(function(){ }); 内。

      我假设validate_form() 函数将在单击任何按钮/超链接时被调用。

      如下:

      var na = null;
      var ea = null;
      var em = null;
      var fn = null;
      var ln = null;
      var eml = null;
      var fnl = null;
      var lnl = null;
      var at = null;
      var per = null;
      
      window.onload = function () {
          na = document.getElementById('nr');
          ea = document.getElementById('er');
          em = document.subscribe.email;
          fn = document.subscribe.fname;
          ln = document.subscribe.lname;
          eml = document.subscribe.email.value.length;
          fnl = document.subscribe.fname.value.length;
          lnl = document.subscribe.lname.value.length;
          at = document.subscribe.email.value.indexOf("@");
          per = document.subscribe.email.value.lastIndexOf(".");
      };
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-31
        • 2017-12-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多