我在jquery中创建了一个联系表单,但是我在显示自定义验证消息时遇到了一些困难。它只显示一个带有消息的小弹出窗口:"请填写表格"而不是我指定的自定义消息。我尝试了不同版本的验证插件,但是没有用。我跟着多个教程都得到了相同的结果。你们当中有些人能把我放在正确的道路上,所以我可以自己完成这个吗?
JQuery的
$(document).ready(function () {
$('#contact_form').validate({
rules: {
name: {
required: true
}
},
messages: {
name: {
required: "come on, you have a name don't you?"
}
},
submitHandler: function (form) {
$(form).ajxSubmit({
type: "POST",
data: $(form).serialize(),
url: "",
success: function () {
$('#contact_form :input').attr('disabled', 'disabled');
$('#contact_form').fadeTo("slow", 0.15, function () {
$(this).find(':input').attr('disabled', 'disabled');
$(this).find('label').css('cursor', 'default');
$('#success').fadeIn();
});
},
error: function () {
$('#contact_form').fadeTo("slow", 0.15, function () {
$('#error').fadeIn();
});
}
});
}
});
});
HTML
<form method="post" name="contact" id="contact_form">
<div class="row uniform collapse-at-2">
<div class="6u">
<input type="text" name="name" id="name" placeholder="Name" required="" />
</div>
<div class="6u">
<input type="email" name="email" id="email" placeholder="Email" required="" />
</div>
</div>
<div class="row uniform">
<div class="12u">
<input type="text" name="subject" id="subject" placeholder="Subject" required="" />
</div>
</div>
<div class="row uniform">
<div class="12u">
<textarea name="message" id="message" placeholder="Message" rows="6" required=""></textarea>
</div>
</div>
<div class="row uniform">
<div class="12u">
<ul class="actions">
<li>
<input type="submit" class="special" value="Send Message" id="submit" name="submit" />
</li>
<li>
<input type="reset" name="reset" id="reset" value="Reset Form" /></li>
</ul>
</div>
</div>
</form>
答案 0 :(得分:0)
下面的代码似乎对我来说很好。确保在所有依赖库之前包含jQuery主库(例如自动完成或其他插件)
<!DOCTYPE html>
<html lang="en">
<head>
<title>jQuery Autocomplete</title>
<meta charset="utf-8">
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.11.1/jquery.validate.min.js"></script>
<script>
$(document).ready(function () {
$('#contact_form').validate({
rules: {
name: {
required: true
}
},
messages: {
name: {
required: "come on, you have a name don't you?"
}
},
submitHandler: function (form) {
$(form).ajxSubmit({
type: "POST",
data: $(form).serialize(),
url: "",
success: function () {
$('#contact_form :input').attr('disabled', 'disabled');
$('#contact_form').fadeTo("slow", 0.15, function () {
$(this).find(':input').attr('disabled', 'disabled');
$(this).find('label').css('cursor', 'default');
$('#success').fadeIn();
});
},
error: function () {
$('#contact_form').fadeTo("slow", 0.15, function () {
$('#error').fadeIn();
});
}
});
}
});
});
</script>
</head>
<body>
<form method="post" name="contact" id="contact_form">
<div class="row uniform collapse-at-2">
<div class="6u">
<input type="text" name="name" id="name" placeholder="Name" required="" />
</div>
<div class="6u">
<input type="email" name="email" id="email" placeholder="Email" required="" />
</div>
</div>
<div class="row uniform">
<div class="12u">
<input type="text" name="subject" id="subject" placeholder="Subject" required="" />
</div>
</div>
<div class="row uniform">
<div class="12u">
<textarea name="message" id="message" placeholder="Message" rows="6" required=""></textarea>
</div>
</div>
<div class="row uniform">
<div class="12u">
<ul class="actions">
<li>
<input type="submit" class="special" value="Send Message" id="submit" name="submit" />
</li>
<li>
<input type="reset" name="reset" id="reset" value="Reset Form" /></li>
</ul>
</div>
</div>
</form>
</body>
</html>