addeventlistener多个功能

时间:2016-02-10 17:25:42

标签: javascript loops addeventlistener

我试图让这段代码发挥作用。程序只读取前两个函数的问题,因为它们在同一个网页中。当我移动到另一个时,没有读取其他功能。有什么帮助吗?

   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;

0 个答案:

没有答案