本文介绍了如何验证两个可能的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获得以下功能以验证2个值。例如,将表格输入与man和stevesho.com匹配,而不仅仅是man。谢谢。

How do I get the function below to Validate against 2 values. e.g Match the form input against "man" and "stevesho.com" not just "man". Thanks.

function access()
{
    if(document.getElementById('letters').value=='man')
        location.href='http://www.google.com'
    else if(document.getElementById('letters').value=='woman')
        location.href='http://www.dynamicdrive.com'
    else if(document.getElementById('letters').value=='cat')
        location.href='http://www.youtube.com'
    else if(document.getElementById('letters').value=='dog')
        location.href='http://www.dailymotion.com'
    else alert('Access denied. Try again.')
}


推荐答案

function access()
{  
    var letters = document.getElementById('letters').value;  
    var goto_link = "";
    switch(letters){  
        case 'man': 
        case 'stevesho.com':
            goto_link = 'http://www.google.com';
            break;
        case 'woman':
            goto_link = 'http://www.dynamicdrive.com';
            break;
        case 'cat':
            goto_link = 'http://www.youtube.com';
            break;
        case 'dog':
            goto_link = 'http://www.dailymotion.com';
            break;
        default: 
            alert('Access denied. Try again.')
            return;
    }
    location.href = goto_link;
}

这篇关于如何验证两个可能的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 17:21