Javascript addeventlistener多重函数

Javascript addeventlistener多重函数,javascript,loops,addeventlistener,Javascript,Loops,Addeventlistener,我正在努力使这个代码正常工作。问题是程序只读取前两个函数,因为它们位于同一网页中。当我移动到另一个函数时,不会读取其他函数。有什么帮助吗 function addListeners(){ if(window.addEventListener){ var elements=['question','pregunta','express','login','repassword']; var events=['keyup','submit','click

我正在努力使这个代码正常工作。问题是程序只读取前两个函数,因为它们位于同一网页中。当我移动到另一个函数时,不会读取其他函数。有什么帮助吗

   function addListeners(){
    if(window.addEventListener){
        var elements=['question','pregunta','express','login','repassword'];
        var events=['keyup','submit','click','submit','submit'];
        var functions=[press,validateQuestion,validateExpress,validateLogin,validateRepassword];
        for(var i=0;i<elements.length;i++)
        {
            document.getElementById(elements[i]).addEventListener(events[i],functions[i],false);
        }
    } else if(window.attachEvent){ // Add for Internet Explorer previous to IE9
        var elements=['question','pregunta','express','login','repassword'];
        var events=['onkeyup','onsubmit','onclick','onsubmit','onsubmit'];
        var functions=[press,validateQuestion,validateExpress,validateLogin,validateRepassword];
        for(var i=0;i<elements.length;i++)
        {
            document.getElementById(elements[i]).attachEvent(events[i],functions[i]);
        }
    }   
    //Add functions
    function press()
    {
        var canti=document.getElementById('question').value.length;
        var disponibles=150-parseInt(canti);
        document.getElementById('cantidad').innerHTML=disponibles;
    }
    function validateQuestion(evt)
    {
        var mail=document.getElementById('email').value;
        var pattern=/^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$/i;
        var captcha=document.getElementById('rcaptcha');
        if(!pattern.test(mail))
        {
            if(country == 'VE')
            {
                alert('Debes ingresar una direcci\xF3n de email v\xE1lida como \'ejemplo@email.com\'.');
            }
            else
            {
                alert('You must enter a valid email address like \'example@email.com\'.');
            }
            evt.preventDefault();
        }
        else
        {
            if(captcha == false)
            {
                if(country == 'VE')
                {
                    alert('Por favor verifica la forma del captcha.');
                }
                else
                {
                    alert('Please check the the captcha form.');
                }
                evt.preventDefault();
            }
        }
    }
    function validateExpress(evt)
    {
        var email=document.getElementById('mail').value;
        var pattern=/^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$/i;
        if(!email)
        {
            if(country=='VE')
            {
                alert('Debes ingresar tu email.');
            }
            else
            {
                alert('You must enter your email.');    
            }
            evt.preventDefault();
        }
        else
        {
            if(!pattern.test(email))
            {
                if(country=='VE')
                {
                    alert('Debes ingresar una direcci\xF3n de email v\xE1lida como \'ejemplo@email.com\'.');
                }
                else
                {
                    alert('You must enter a valid email address like \'example@email.com\'.');
                }
                evt.preventDefault();
            }
            else
            {
                return true;    
            }
        }
    }
    function validateLogin(evt)
    {
        var mail=document.getElementById('nombre').value;
        var pattern=/^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$/i;
        if(!pattern.test(mail))
        {
            if(country == 'US')
            {
                alert('Debes ingresar una direcci\xF3n de email v\xE1lida (correcto: \'ejemplo@email.com\'; incorrecto: \'ejemploemail.com\' o \'ejemplo@email\').');
            }
            else
            {
                alert('You must enter a valid email address (correct: \'example@email.com\'; incorrect: \'exampleemail.com\' or \'example@email\').');
            }
            evt.preventDefault();
        }
    }
    function validateRepassword(evt)
    {
        var mail=document.getElementById('mail').value;
        var pattern=/^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$/i;
        if(!pattern.test(mail))
        {
            if(country == 'VE')
            {
                alert('Debes ingresar una direcci\xF3n de email v\xE1lida (correcto: \'ejemplo@email.com\'; incorrecto: \'ejemploemail.com\' o \'ejemplo@email\').');
            }
            else
            {
                alert('You must enter a valid email address (correct: \'example@email.com\'; incorrect: \'exampleemail.com\' or \'example@email\').');      
            }
            evt.preventDefault();
        }
    }
}
window.onload=addListeners;
函数addListeners(){
if(window.addEventListener){
var元素=['question'、'pregunta'、'express'、'login'、'repassword'];
var事件=['keyup'、'submit'、'click'、'submit'、'submit'];
var函数=[按,validateQuestion,validateExpress,validateLogin,ValidatePassword];

对于(var i=0;我必须澄清,
'question'、'pregunta'
在一个页面上,以及
'express'、'login'、'repassword'
在其他页面上,并且这两个页面上都有代码?如果您打开开发人员工具(通常是F12),您是否看到
addEventListener
不是
null
上的属性时出现错误?是的,前两个函数在同一页中,它们工作正常。程序不会读取其他页中的函数。我测试了所有函数,它们运行正常,但当我试图使它们一起运行时不工作:(此代码位于.js外部文件中!顺便说一句!您能告诉我们如何加载.js文件吗?上面的代码是包含所有javascript代码的.js文件。我还有更多代码要添加,但需要确保这些函数首先工作。我调用了文档标题中的.js文件,这对于所有类似这样的页面都是一样的: