jQuery脚本没有正确引用
问题描述:
我不明白为什么,但我的脚本工作不正常,因为它以前工作正常。现在,它不再工作了,我不知道为什么,因为我不记得对脚本标记进行任何更改。jQuery脚本没有正确引用
<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');
}
});
});
答
不是说它应该完全因为这个原因而失败,但是你在phoneCheck()函数中的else if中缺少返回值。
除此之外它看起来很好imo。
您是否尝试向上移动您的 code to最佳? – zkanoca 2013-03-20 21:09:46
什么工作不正常?如果你的意思是你的表单没有被提交,你没有在第2行表明表单的行为 – zkanoca 2013-03-20 21:12:54
@Adam Ginther因为你的代码实际上并未包含在脚本标记中?还是只是在代码块底部的外部文件中发布代码? – VoidKing 2013-03-20 21:12:59