Jak uzyskać poniżej funkcję, aby zatwierdzić w stosunku do dwóch wartości. na przykład Dopasuj wejście formularza przeciwko "człowiek" i "Stevesho.com" nie tylko "człowiek".

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.')
}
0
Dz.slick 10 listopad 2011, 11:50

3 odpowiedzi

Najlepsza odpowiedź
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;
}
0
Rodolphe 10 listopad 2011, 15:32

Szukasz operatora lub operatora? Zobacz http://www.w3schools.com/js/js_comparisons.asp

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

if (letter == 'man' || letter == 'stevesho.com')
    location.href = 'http://www.google.com'

Inna opcja, która może być interesująca;

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.");
}
0
Stefan 10 listopad 2011, 09:27

To może być:

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.')
}
0
Rodolphe 2 lipiec 2019, 07:30