【发布时间】:2017-04-27 16:36:20
【问题描述】:
我正在使用jquery validate插件,当表单提交时,样式丢失了。
他们只是变得古怪!
我希望表单在提交表单之前保持原样。
附上截图和代码:
<script src="https://cdn.jsdelivr.net/jquery.validation/1.15.1/jquery.validate.min.js"></script>
<style>
.label {
width:100px;
text-align:right;
float:left;
padding-right:10px;
font-weight:bold;
}
#ats_fellow_application_payment_form label.error {
color:#FB3A3A;
font-weight:bold;
padding: 10px;
}
#ats_fellow_application_payment_form input.error {
color:#FB3A3A;
font-weight:bold;
padding: 0px;
}
h1 {
font-family: Helvetica;
font-weight: 100;
color:#333;
padding-bottom:20px;
}
</style>
<script type="text/javascript">
// Wait for the DOM to be ready
$(function() {
// Initialize form validation on the registration form.
// It has the name attribute "registration"
$("form[name='ats_fellow_application_payment_form']").validate({
// Specify validation rules
rules: {
// The key name on the left side is the name attribute
// of an input field. Validation rules are defined
// on the right side
firstname: "required",
lastname: "required",
email: {
required: true,
// Specify that email should be validated
// by the built-in "email" rule
email: true
}
},
// Specify validation error messages
messages: {
firstname: "Please enter your firstname",
lastname: "Please enter your lastname",
email: "Please enter a valid email address"
},
// Make sure the form is submitted to the destination defined
// in the "action" attribute of the form when valid
submitHandler: function(form) {
form.submit();
}
});
});
</script>
<form method="POST" name="ats_fellow_application_payment_form" id="ats_fellow_application_payment_form" novalidate="novalidate">
<div class="label">First Name</div><input type="text" id="firstname" name="firstname" /><br />
<div class="label">Last Name</div><input type="text" id="lastname" name="lastname" /><br />
<div class="label">Email</div><input type="text" id="email" name="email" /><br />
<div style="margin-left:140px;"><input type="submit" name="submit" value="Submit" /></div>
</form>
【问题讨论】:
-
我认为这是由 validate 插件添加的错误
label元素引起的。我建议您查看验证插件允许您使用的错误自定义方法 -
您的 OP 中的 CSS 代码不会重现您描述的问题:jsfiddle.net/j6v3x378 ~ 但是,jQuery Validate 插件根本不会影响 CSS;它只是为错误消息添加/删除类和
label元素。检查 DOM 并创建正确的 CSS 完全取决于您。
标签: jquery css jquery-validate