1

このプログラムを実行すると、validatePhone();、validateAddress(); およびvalidateCity(); は完全にスキップされます。なぜですか? ここに私のJSがあります:

function validatePage()
{
    var valid = false;//sets valid.
    var msg = "";//sets message to blank.
    validateFname();
    function validateFname()
    {
        var fnameTxt = /^[a-zA-Z]+$///sets valid inputs for recipient name.
        if(firstName.value.match(fnameTxt))//checks if there has been an entered value, then sets valid to true.
        {
            valid = true;
        }
        else
        {
            valid = false;
        }
        validateLname();
    }
    function validateLname()
    {
        var lnameTxt = /^[a-zA-Z]+$///sets valid inputs for recipient name.
        if(lastName.value.match(lnameTxt))//checks if there has been an entered value, then sets valid to true.
        {
            valid = true;
        }
        else
        {
            valid = false;
        }
        validatePhone();
    }
    function validatePhone()
    {
        var nameTxt = /^[0-9]+$///sets valid inputs for recipient name.
        if(Phone.value.match(nameTxt))//checks if there has been an entered value, then sets valid to true.
        {
            valid = true;
        }
        else
        {
            valid = false;
        }
        validateAddress();
    }
    function validateAddress()
    {
        var addressTxt = /^[0-9a-zA-Z]+$///sets valid inputs for recipient name.
        if(address1.value.match(addressTxt))//checks if there has been an entered value, then sets valid to true.
        {
            valid = true;
        }
        else
        {
            valid = false;
        }
        validateCity();
    }
    function validateCity()
    {
        var cityTxt = /^[a-zA-Z]+$///sets valid inputs for recipient name.
        if(cityTown.value.match(cityTxt))//checks if there has been an entered value, then sets valid to true.
        {
            valid = true;
        }
        else
        {
            valid = false;
        }
        validatePostcode();
    }
    function validatePostcode()
    {
        var postcodeTxt = /^[0-9a-zA-Z]+$///sets valid inputs for recipient name.
        if(postcode.value.match(postcodeTxt))//checks if there has been an entered value, then sets valid to true.
        {
            valid = true;
        }
        else
        {
            valid = false;
        }
    }
    if(valid == true)
    {
        window.open("checkout_step_5.html");
    }
    else
    {
        msg="Not all required fields were filled."
        alert(msg);
        return false;
    }
}

スペルミスをチェックしましたが、気づいたものはありません。なぜこれが機能しないのか本当にわかりませんか?

4

1 に答える 1

0

コード全体を try catch に入れてみてください。今のところ、例外は発生しませんでした

    <!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function validatePage()
{
try{
    var valid = false;//sets valid.
    var msg = "";//sets message to blank.
    validateFname();
    function validateFname()
    {
        var fnameTxt = /^[a-zA-Z]+$///sets valid inputs for recipient name.
        if(firstName.value.match(fnameTxt))//checks if there has been an entered value, then sets valid to true.
        {
            valid = true;
        }
        else
        {
            valid = false;
        }
        validateLname();
    }
    function validateLname()
    {
        var lnameTxt = /^[a-zA-Z]+$///sets valid inputs for recipient name.
        if(lastName.value.match(lnameTxt))//checks if there has been an entered value, then sets valid to true.
        {
            valid = true;
        }
        else
        {
            valid = false;
        }
        validatePhone();
    }
    function validatePhone()
    {
        var nameTxt = /^[0-9]+$///sets valid inputs for recipient name.
        if(Phone.value.match(nameTxt))//checks if there has been an entered value, then sets valid to true.
        {
            valid = true;
        }
        else
        {
            valid = false;
        }
        validateAddress();
    }
    function validateAddress()
    {
        var addressTxt = /^[0-9a-zA-Z]+$///sets valid inputs for recipient name.
        if(address1.value.match(addressTxt))//checks if there has been an entered value, then sets valid to true.
        {
            valid = true;
        }
        else
        {
            valid = false;
        }
        validateCity();
    }
    function validateCity()
    {
        var cityTxt = /^[a-zA-Z]+$///sets valid inputs for recipient name.
        if(cityTown.value.match(cityTxt))//checks if there has been an entered value, then sets valid to true.
        {
            valid = true;
        }
        else
        {
            valid = false;
        }
        validatePostcode();
    }
    function validatePostcode()
    {
        var postcodeTxt = /^[0-9a-zA-Z]+$///sets valid inputs for recipient name.
        if(postcode.value.match(postcodeTxt))//checks if there has been an entered value, then sets valid to true.
        {
            valid = true;
        }
        else
        {
            valid = false;
        }
    }
    if(valid == true)
    {
        window.open("checkout_step_5.html");
    }
    else
    {
        msg="Not all required fields were filled."
        alert(msg);
        return false;
    }
    }catch(e){
    alert(e);
    }
}


</script>
</head>
<body onload="validatePage()">

</body>
</html>

また、Valid が一度 false になった場合は、return を使用して関数をブレークスルーしてください。1 つのパラメーターが false かどうかを常にチェックすることには意味がありません。

if(firstName.value.match(fnameTxt))//checks if there has been an entered value, then sets valid to true.
    {
        valid = true;
    }
    else
    {
        valid = false;
return;
        }
于 2013-09-16T11:29:19.530 に答える