Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何根据两个可能的值进行验证?_Javascript - Fatal编程技术网

Javascript 如何根据两个可能的值进行验证?

Javascript 如何根据两个可能的值进行验证?,javascript,Javascript,如何让下面的函数根据两个值进行验证。e、 g.将表格输入与man和stevesho.com匹配,而不仅仅是man function access() { if(document.getElementById('letters').value=='man') location.href='http://www.google.com' else if(document.getElementById('letters').value=='woman')

如何让下面的函数根据两个值进行验证。e、 g.将表格输入与man和stevesho.com匹配,而不仅仅是man

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 s = document.getElementById('letters').value;
    if(s =='man' || s = 'stevesho.com')
        location.href='http://www.google.com'
    else if(s =='woman')
        location.href='http://www.dynamicdrive.com'
    else if(s =='cat')
        location.href='http://www.youtube.com'
    else if(s =='dog')
        location.href='http://www.dailymotion.com'
    else alert('Access denied. Try again.')
}

你在找OR接线员吗?看

另一个可能感兴趣的选择

var key = document.getElementById('letters').value

var collection = {
    "key1" : "url1",
    "key2" : "url2",
    "key3" : "url3",
    "man" : "http://www.google.com",
    "stevesho.com" : "http://www.google.com"
};

if (collection[key]) {
    var url = collection[key];
    alert(key + " = " + url); // DEBUG
    //location.href = url;
}
else {
    alert("Access denied. Try again.");
}

你的问题不是很清楚。您的访问功能似乎正确。什么不起作用?关于你在一个分区的警报箱,我不明白。加上div联系表单是什么?我已经编辑了它,忘记了validationTry:var letters=document.getElementById'letters'.value;
var letter = document.getElementById('letters').value

if (letter == 'man' || letter == 'stevesho.com')
    location.href = 'http://www.google.com'
var key = document.getElementById('letters').value

var collection = {
    "key1" : "url1",
    "key2" : "url2",
    "key3" : "url3",
    "man" : "http://www.google.com",
    "stevesho.com" : "http://www.google.com"
};

if (collection[key]) {
    var url = collection[key];
    alert(key + " = " + url); // DEBUG
    //location.href = url;
}
else {
    alert("Access denied. Try again.");
}