如何为多个函数声明变量

发布于 2024-10-06 06:20:21 字数 1303 浏览 0 评论 0原文

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

抱歉,

<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}

当变量位于函数内部时,它工作得很好,但是当它们像这样时,什么也不会发生。

Correct me if I am wrong, but isn't it possible to declare a variable right after the script tag, so that it can be used throughout that script? I tried this, and my functions are acting as if it isn't even there. Did I do something wrong, or is this supposed to happen. I would hate to have to redeclare all of my variables for each function if they are the exact same thing.

Sorry about that

<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}

It works perfectly fine when the vars are INSIDE the function, but when they are like this, nothing at all happens.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不必了 2024-10-13 06:20:22

我实际上已经知道该怎么做了。我刚刚阅读了有关全局变量以及如何在函数内声明它们的内容。所以我把所有的变量放回函数内部,删除了它上面的“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}

I actually figured out how to do it. I just read about the global variables, and how to declare them inside a function. So I put all the vars back inside the function, erased the "var" on it and it works perfect now.

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}
他不在意 2024-10-13 06:20:22

如果您将所有这些代码都包含在 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(".");
};

It will be better if you wrap all your this code inside window.onload event. or inside $(function(){ }); in case of jquery.

I am assuming that validate_form() function will be called on the click of any button/hyperlink.

like following:

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(".");
};
满意归宿 2024-10-13 06:20:21

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

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

我要做的是将分配 DOM 元素的变量保留在函数之外,但将获取 DOM 元素的值/长度的变量移到函数内部。然后您可以引用包含 dom 元素的 var。

例如(部分代码):

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

函数 validate_form() { var eml=em.value.length, fnl=fn.值.长度, lnl=ln.lname.value.length, at=em.value.indexOf("@"), per=em.value.lastIndexOf("."); // 其余代码。

The issue is that the variables (particularly eml, fnl, lnl) contain values obtained when they are declared. JS is not recalculating the length of strings or the values of elements every time your function is called.

When you move those vars inside the function, then they actually are "recalculated" every time the function is called.

What I would do is leave the vars that are assigned DOM elements outside of the functions, but move the vars that get DOM elements' values/lengths inside the functions. Then you can reference the vars that contain the dom elements.

For example (partial code):

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文