正如有人已經提到,你應該看看使用外部庫進行驗證。這就是說,這似乎是它可能工作(see JSFiddle):
var $form = $("#myid"),
$errorMsg = $("<span class='error'>This field is required..!!</span>");
$("#submit").on("click", function() {
// If any field is blank, we don't submit the form
var toReturn = true;
$("input", $form).each(function() {
// If our field is blank
if ($(this).val() == "") {
// Add an error message
if (!$(this).data("error")) {
$(this).data("error", $errorMsg.clone().insertAfter($(this)));
}
toReturn = false;
}
// If the field is not blank
else {
// Remove the error message
if ($(this).data("error")) {
$(this).data("error").remove();
$(this).removeData("error");
}
}
});
return toReturn;
});
這是完全完美〜非常感謝 –