我正在使用 jquery 在我的表单上设置我的错误消息,当我去查看它时,它不会要求输入姓名字段,而只是要求电子邮件,即使我已经包含了它。这是我的代码:
<form method="post" action="" id="contactform">
<fieldset class="first">
<div id="response"></div>
<div class="name_input">
<input name="name" id="name" class="required" type="text" onfocus="if(this.value == 'Name'){this.value = '';}" onblur="if(this.value == ''){this.value='Name';}" value="Name" maxlength="128" />
</div>
<div id="email_input">
<input id="email" name="email" onfocus="if(this.value == 'Email'){this.value = '';}" onblur="if(this.value == ''){this.value='Email';}" class="required" type="text" value="Email" maxlength="128" />
</div>
<div id="button">
<input type="submit" class="button" name="Submit" value="" />
</div>
</fieldset>
</form>
和我的 JS:
$(document).ready(function() {
$('form #response').hide();
$('#button').click(function(e) {
e.preventDefault();
var valid = '';
var required = ' is required.';
var name = $('form #name').val();
var email = $('form #email').val();
if (name == '' || name.length < 2) {
valid = '<p>Your name' + required +'</p>';
}
if (!email.match(/^([a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4}$)/i)) {
valid += '<p>Your email' + required +'</p>';
}
if (valid != '') {
$('form #response').removeClass().addClass('error')
.html('<strong>Please correct the errors below.</strong>' +valid).fadeIn('fast');
}
else {
$('form #response').removeClass().addClass('processing').html('Processing...').fadeIn('fast');
var formData = $('form').serialize();
submitForm(formData);
}
});
});
我的姓名标签在其他任何地方都没有任何冲突,所以我不知道会发生什么