作成者とメッセージの2つのフィールドを含むコメントフォームがあります。送信ボタンをクリックしても、検証は実行されません。送信ボタンをもう一度クリックすると、検証が機能します。なぜですか?
//User Clicked Submit Button
$('#CommentsForm').live('submit', function (e) {
e.preventDefault(); //Prevent Browser from getting new url
//Validate the comments form
$('#CommentsForm').validate({
rules: {
Author: "required",
Message: "required"
},
messages: {
Author: "Author is required.",
Message: "Comment is required."
},
errorContainer: "#CommentsErrorBox",
errorLabelContainer: "#CommentsErrorBox ul",
wrapper: "li",
submitHandler: function() {
//Create JSON object
var jsonCommentData = {
ProductID: $('#HiddenProductID').val(),
Author: $('#Author').val(),
Message: $('#Message').val()
};
//Add the comment.
$.ajax({
url: '/Home/_CommentsAdd',
type: 'POST',
data: JSON.stringify(jsonCommentData),
dataType: 'html',
contentType: 'application/json',
//The request was a success. Repopulate the div with new result set.
success: function (data) {
$('#AjaxComments').html(data);
$('abbr.timeago').timeago(); //update the timestamp with timeago
//Change colors of message.
if ($('#CommentStatus').html() == "Your Comment Has Been Added!") {
$('#CommentStatus').css('color', 'GREEN');
}
},
error: function (data) {
alert('Fail');
}
});
}
});
});