jQuery 验证元素背景颜色的变化

发布于 2024-09-25 07:17:40 字数 1823 浏览 0 评论 0原文

默认情况下,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>

其中<当发生错误时,code>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;}

Chrome 中的 CSS 屏幕截图

jQuery.validate doesn't seem to change the background color of the invalid element by default. Is it also possible to change the background color of a select element? Most of my elements are input type="text", but I need an indicator for the select form elements. I am not using the default generated messages, as they don't fit my layout.

The following code does change the background color of the input elements, but it never reverts to its previous style:

 $('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');
        }
    });

Edit (sample)

<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>

Where the input element is given the jqInvalid error class on error.

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;}

CSS screenshot in Chrome

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月竹挽风 2024-10-02 07:17:40

无效元素默认会得到一个 error默认 ,或者在您的情况下 jqInvalid 因为您已经设置了 errorClass 选项,只需在样式表中按照您想要的方式设置该类的样式,例如:

.jqInvalid { background: #ffdddd; }

您可以覆盖如果需要,错误消息(默认元素为 ):

label.jqInvalid { background: none; }

此 CSS 方法负责删除...因为类一旦有效就会被删除。如果您希望将绿色背景应用于有效元素等,还有一个 success 类。

The invalid elements get a class of error by default, or in your case jqInvalid since you've set the errorClass option, just style that class like you want in the stylesheet, for example:

.jqInvalid { background: #ffdddd; }

You can override the specifics for the error messages (the default element being <label>) if you want:

label.jqInvalid { background: none; }

This CSS approach takes care of the removal...since the class is removed once it's valid. There's also a success class if you want a green background applied to valid elements, etc.

所有深爱都是秘密 2024-10-02 07:17:40

这是一个特异性问题。 .error.jqInvalid 都没有 .field .txt 具体,

因此,增加特异性需要将 CSS 更改为 .field input.error,它比 .field .txt 更重要,因此顺序不再重要。

有关特异性的信息,请参阅此处

This was a specificity problem. .error and .jqInvalid are both less specific than .field .txt

So, increasing the specificity required changing the CSS to .field input.error, which has more weight than .field .txt so the order no longer mattered.

See here for info on specificity

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文