我正在尝试使用jQuery验证表单(所有字段)
我的代码仅对输入区域有效,但对<textarea>
不起作用,有人知道我如何解决此问题?
$(".button").click(function() {
// validate and process form
// first hide any error messages
$('.error').hide();
var name = $("input#name").val();
if (name == "") {
$("label#name_error").show();
$("input#name").focus();
return false;
}
var email = $("input#email").val();
if (email == "") {
$("label#email_error").show();
$("input#email").focus();
return false;
}
var subject = $("input#subject").val();
if (subject == "") {
$("label#subject_error").show();
$("input#subject").focus();
return false;
}
var message = $("textarea#message").val();
if (message == "") {
$("label#message_error").show();
$("textarea#message").focus();
return false;
}
var dataString = 'name='+ name + '&email=' + email + '&subject=' + subject + '&message=' + message;
//alert (dataString);return false;
$.ajax({
type: "POST",
url: "bin/process.php",
data: dataString,
success: function() {
$('#contact_form').html("<div id='message'></div>");
$('#message').html("<h2>Mensagem enviada com sucesso!</h2>")
.append("<p>Entraremos em contato assim que possível.</p>")
.hide()
.fadeIn(1500, function() {
$('#message').append("<img id='checkmark' />");
});
}
});
return false;
});
})
最佳答案
<script type="text/javascript">
$(document).ready(function () {
$("#submit").validate({
rules:{
textarea_name:{
required:true,
minlength:8
}
}
});
});
</script>