Dav*_*Fox 8 css jquery jquery-plugins jquery-validate
默认情况下,jQuery.validate似乎不会更改无效元素的背景颜色.是否也可以更改select元素的背景颜色?我的大多数元素都是input type="text",但我需要一个指定选择表单元素.我没有使用默认生成的消息,因为它们不适合我的布局.
以下代码确实更改了input元素的背景颜色,但它永远不会恢复为以前的样式:
$('form[name="register"]').validate({
errorClass: 'jqInvalid',
rules: {
fnameCreate: "required", //input
monthCreate: "required", //select
emailCreate: { required: true, email: true }, //input
passwordCreate: "required", //input type=password
},
messages: {
fnameCreate: "",
monthCreate: "",
emailCreate: "",
passwordCreate: "",
},
errorPlacement: function (error, element) {
element.css('background', '#ffdddd');
}
});
编辑(样本)
<div class="field">
<div class="labelName_fb">
<label for="email">Email</label>
</div>
<div class="divforText3">
<div class="divLeft"></div>
<div class="textbox-image3">
<input class="txt required" id="emailCreate" name="emailCreate" value="" maxlength="100" type="text" style='width:180px'>
</div>
<div class="divright"></div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
在错误的情况下,input元素被赋予jqInvalid错误类.
CSS
/* line 3 */ .error { background: #ffdddd; } /* I have since removed the errorClass option */
/* line 254 */ .field .txt {background:transparent none repeat scroll 0 0;border:medium none;color:#000;font-size:11px;width:130px; margin:5px 0;}
Run Code Online (Sandbox Code Playgroud)

error 默认情况下,无效元素会获得一个类,或者在您jqInvalid设置errorClass选项的情况下,只需在样式表中为该类设置样式,例如:
.jqInvalid { background: #ffdddd; }
Run Code Online (Sandbox Code Playgroud)
如果需要,您可以覆盖错误消息的细节(默认元素为<label>):
label.jqInvalid { background: none; }
Run Code Online (Sandbox Code Playgroud)
这种CSS方法负责删除...因为类一旦有效就被删除.success如果您想要将绿色背景应用于有效元素等,还有一个类.