0

我不知道为什么,但我的脚本不能正常工作,因为它以前工作正常。现在,它不再工作了,我不知道为什么,因为我不记得对脚本标签进行了任何更改。

<body>
<form id="contact">
    <fieldset id="contactInformation">
        <legend>Contact Information</legend>
        <p id="error"></p>
        <label for="name">Name:</label>
        <br>
        <input id="name" type="text">
        <br>
        <br>
        <label for="phoneNumber">Phone Number:</label>
        <br>
        <input id="phoneNumber" type="text">
        <br>
        <br>
        <label id="eMail">E-Mail Address:</label>
        <br>
        <input type="text">
        <br>
        <br>
        <label id="address">Address:</label>
        <br>
        <input type="text">
        <br>
        <br>
        <label id="city">City:</label>
        <br>
        <input type="text">
        <br>
        <br>
        <label id="postalCode">Postal Code:</label>
        <br>
        <input type="text">
        <br>
        <br>
        <label id="province">Province</label>
        <br>
        <select id="province">
            <option>Choose Your Province</option>
            <option>Alberta</option>
            <option>British Columbia</option>
            <option>Manitoba</option>
            <option>New Brunswick</option>
            <option>Newfoundland and Labrador</option>
            <option>Northwest Territories</option>
            <option>Nova Scotia</option>
            <option>Nunavut</option>
            <option>Ontario</option>
            <option>Prince Edward Island</option>
            <option>Quebec</option>
            <option>Saskatchewan</option>
            <option>Yukon Territory</option>
        </select>
        <br>
        <br>
        <div id="shipping">
            <label id="shippingCheckbox">Is your shipping information the same as your contact information?</label>
            <br>
            <br>
            <input type="checkbox" id="sameInfo">
            <label>Yes it is</label>
            <br>
            <br>
        </div>
        <fieldset id="shippinginformation">
            <legend>Shipping Information</legend>
            <label for="phoneNumber">Phone Number:</label>
            <br>
            <input id="phoneNumber" type="text">
            <br>
            <br>
            <label id="address">Address:</label>
            <br>
            <input type="text">
            <br>
            <br>
            <label id="postalCode">Postal Code:</label>
            <br>
            <input type="text">
            <br>
            <br>
        </fieldset>
        <br>
        <input type="submit" id="submit" name="submit" value="Submit">
</form>
<br>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="js/script.js"></script>
</body>
</html>

//script.js
var errorNotify = 'Sorry, we could not process your request because '
var errorField = [];

function formVal() {
    var isValid = true;

    for (var i = 0; i < errorField.length; i++ ) {
        $(errorField[i]).removeClass('error');
    }
    errorField = [];

    if(!nameCheck()) {
        isValid = false;
    }
    if(!phoneCheck()) {
        isValid = false;
    }
    if (isValid === false) {
        $('#error').html(errorNotify).hide().slideDown('slow');
        for (var i = 0; i < errorField.length; i++) {
            $(errorField[i]).addClass('error');
        }
        errorNotify = 'Sorry, we could not process your request because ';
    }

    function nameCheck(){
        if ($('#name').val() === '') {
            errorNotify += ' you did not enter your name.';
            errorField.push($('#name'));
            return false;
        } else {
            return true;
        }
    }
    function phoneCheck(){
        var phoneCheck = $('#phoneNumber').val();
        if (phoneCheck === '') {
            errorNotify += ' you did not enter your phone number.';
            errorField.push($('#phoneNumber'));
            return false;
        } else if (phoneCheck.length !== 10) {
            errorNotify += 'please enter a 10 digit phone number';
            errorField.push($('#phoneNumber'));
        }
        else if (isNaN(parseInt(phoneCheck))) {
            errorNotify += 'you have entered a letter, please enter a number';
            errorField.push($('#phoneNumber'));
        }
        else {
            return true;
        }
    }
    return isValid;
}

$(function () {

    $('#contact').submit(function() {
        return formVal();
    });

    $('#sameInfo').change(function() {
        if ($(this).is(":checked")) {
            alert('this is a test');
        } else {
            alert('this is not a test');
        }
    });
});
4

1 回答 1

0

并不是说它应该因此而完全失败,但是您的 else if 在 phoneCheck() 函数中缺少返回值。

除此之外,它看起来还不错。

于 2013-03-20T21:33:03.450 回答