2010-12-05 3 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

우리는 * 코드 : –

+0

코드 바랍니다 게시 항상 *, 코드없이 추측하고 있습니다 .... –

답변

1

나는 실제로 그것을 수행하는 방법을 알아 냈어. 방금 전역 변수와 함수 안에 선언하는 방법에 대해 읽었습니다. 그래서 모든 vars를 함수 안에 넣고 "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

문제는 변수 (특히 발광층, FNL, LNL)들이 선언 될 때 얻어진 값을 포함한다는 것이다. JS는 함수가 호출 될 때마다 문자열의 길이 또는 요소의 값을 다시 계산하지 않습니다.

함수 내에서 변수를 이동하면 함수가 호출 될 때마다 실제로 변수가 "다시 계산됩니다".

내가 할 수있는 것은 함수 밖에서 DOM 요소가 할당 된 변수를 남기고 함수 내에서 DOM 요소의 값/길이를 가져 오는 변수를 이동하는 것입니다. 그런 다음 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.

관련 문제