2010-12-05 13 views
1

如果我错了,请纠正我,但无法在脚本标记后面声明变量,以便可以在整个脚本中使用该变量吗?我试过了,我的功能就好像它不在那里一样。我做错了什么,或者这是否应该发生?如果它们完全相同,我不愿意重新声明每个函数的所有变量。如何为多个函数声明变量

对此深感抱歉

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

当瓦尔都在里面的功能它工作完全正常,但是当他们都是这样的,什么都没有发生。

+0

我们猜测没有代码,*总是*发布代码:) – 2010-12-05 09:26:19

回答

1

其实我想通了,如何做到这一点。我刚刚阅读了全局变量,以及如何在函数中声明它们。所以我把所有的变量都放回函数中,删除了它上面的“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} 
0

如果您将所有此代码包装在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("."); 
}; 
1

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

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

我所要做的就是将赋予DOM元素的变量放在函数之外,但移动在函数内部获取DOM元素的值/长度的变量。然后你可以引用包含dom元素的变量。

例如(部分代码):

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.