jQuery脚本没有正确引用

时间:2013-03-20 21:07:02

标签: javascript jquery html css

我无法弄清楚为什么但是我的脚本工作不正常,因为之前工作正常。现在,它不再起作用,我无法弄清楚为什么我不记得对脚本标签进行任何更改。

<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');
        }
    });
});

1 个答案:

答案 0 :(得分:0)

并不是说它应该完全失败,但是如果在phoneCheck()函数中,你的遗漏值就会丢失。

除此之外看起来很好。