addClass 用 JQuery Validation 突出显示替换现有类

发布于 2024-12-04 21:58:20 字数 5118 浏览 1 评论 0原文

我正在使用 JQuery 验证插件,我的目标是对无效字段的标签应用错误类。我设法做到了这一点,但我的问题是 JQuery 正在覆盖标签的现有类,而不是向其附加错误类。

这是 JQuery:

<script src="<?php bloginfo('template_directory'); ?>/js/lib/jquery.js" type="text/javascript"></script>
<script src="<?php bloginfo('template_directory'); ?>/js/jquery.validate.js" type="text/javascript"></script>
<script>
    $(document).ready(function(){
        $("#profile-basic").validate({
            debug:true,
            highlight: function(element, errorClass, validClass) {
                $(element).addClass(errorClass).removeClass(validClass);
                $(element.form).find("label[for=" + element.id + "]")
                .addClass(errorClass);
            },
            unhighlight: function(element, errorClass, validClass) {
                $(element).removeClass(errorClass).addClass(validClass);
                $(element.form).find("label[for=" + element.id + "]")
                .removeClass(errorClass);
            }
        });
    });
</script>

突出显示和取消突出显示选项来自 JQuery 网站。 (http://docs.jquery.com/Plugins/Validation/validate

这是HTML - 有问题的元素是标签并选择类别ID:

<form method="post" action="" class="bullhorn" id="profile-basic">
    <div id="accordion">  <!-- Accordion is currently disabled -->
        <h3>Name*</h3>
        <div>
            <div class="twocol">
                <label for="firstName">First Name:*</label>
                <input type="text" id="firstName" name="firstName" class="required" />

                <label for="middleName">Middle Name:</label>
                <input type="text" id="middleName" name="middleName" />

                <label for="lastName">Last Name:*</label>
                <input type="text" id="lastName" name="lastName" class="required" />
            </div>
            <div class="twocol">
                <label for="categoryID" class="ownline">Please select your primary category:*</label>
                <select name="categoryID" id="categoryID" class="required">
                    <option value="">Category 1</option>
                    <option value="">Category 2</option>
                    <option value="">Category 3</option>
                    <option value="">Category 4</option>
                    <option value="">Category 5</option>
                    <option value="">Category 6</option>
                    <option value="">Category 7</option>
                    <option value="">Category 8</option>
                    <option value="">and so on</option>
                </select>
            </div> 
        </div><!-- name -->
    </div>
    <div class="buttons"><input type="submit" value="Save and Go to Work History"/><input type="submit" value="Save"/></div>
</form>

请注意,手风琴当前已禁用 - 已完全注释掉。

以及相关的 CSS:

label { float:left; text-align:right; width:30%; margin:0 14px 14px 0; line-height:20px; font-weight:bold; }
input, select, textarea { float:left; width:63%; margin-bottom:14px; background:#eee; line-height:1; }
    input[type=submit],  input[type=reset] { float:right; display:block; width:auto; margin:20px 0 0 14px; }
input:focus, textarea:focus { background:#fff; }
input, textarea { padding:2px 4px; }
select option { text-transform:capitalize; }
textarea { height:49px; }

.ownline { text-align:left; width:100%; line-height:20px; font-weight:bold; margin-bottom:3px; }
    .ownline+input,  .ownline+select,  .ownline+textarea, .ownline+.fieldinstructions+select { width:100%; }

label.error { color:#8e082d; }
    label.error:before { content: "\00bb\00a0"; display:inline-block; text-indent:-10px; }
input.error { border:2px solid #8e082d; background-color:#fdf5f3; } 

当我提交表单时(没有填写字段,因此会生成错误)categoryID 标签上的类 ownline 被删除并替换为 error班级。至少看起来是这样。当我查看页面的源代码时,categoryID 标签如下所示: (为了简洁,我删除了选择选项。)

<label for="categoryID" class="ownline">Please select your primary category:*</label>
<select name="categoryID" id="categoryID" class="required">
</select>

但在 Firebug 中,它看起来像:

<label class="error" for="categoryID">Please select your primary category:*</label>
<select id="categoryID" class="required error" name="categoryID">
</select>

有趣的是,JQuery 覆盖了标签中的现有类,但附加到选择中的现有类。

如果这是 CSS 特异性问题,我测试了使用 label.ownline 作为选择器编写 ownline 样式,但没有解决问题。

这是一个安装了联系表单 7 的 WordPress 网站,但是 我添加了一个function 到functions.php,这样联系表单就不会在相关页面上调用其内容

我已经用谷歌搜索这个几个小时了。

我绝对是一个 JQuery 菜鸟。我很想得到一些帮助,但请慢慢说。 :)

谢谢你,谢谢你, 金

I am using the JQuery Validation plug-in and my goal is for the labels of invalid fields to have the error class applied to it. I managed to do this, but my problem is that JQuery is overwriting the label's existing class rather than appending the error class to it.

Here's the JQuery:

<script src="<?php bloginfo('template_directory'); ?>/js/lib/jquery.js" type="text/javascript"></script>
<script src="<?php bloginfo('template_directory'); ?>/js/jquery.validate.js" type="text/javascript"></script>
<script>
    $(document).ready(function(){
        $("#profile-basic").validate({
            debug:true,
            highlight: function(element, errorClass, validClass) {
                $(element).addClass(errorClass).removeClass(validClass);
                $(element.form).find("label[for=" + element.id + "]")
                .addClass(errorClass);
            },
            unhighlight: function(element, errorClass, validClass) {
                $(element).removeClass(errorClass).addClass(validClass);
                $(element.form).find("label[for=" + element.id + "]")
                .removeClass(errorClass);
            }
        });
    });
</script>

The highlight and unhighlight options came off the JQuery web site. (http://docs.jquery.com/Plugins/Validation/validate)

Here's the HTML - the elements in question are the label and select for categoryID:

<form method="post" action="" class="bullhorn" id="profile-basic">
    <div id="accordion">  <!-- Accordion is currently disabled -->
        <h3>Name*</h3>
        <div>
            <div class="twocol">
                <label for="firstName">First Name:*</label>
                <input type="text" id="firstName" name="firstName" class="required" />

                <label for="middleName">Middle Name:</label>
                <input type="text" id="middleName" name="middleName" />

                <label for="lastName">Last Name:*</label>
                <input type="text" id="lastName" name="lastName" class="required" />
            </div>
            <div class="twocol">
                <label for="categoryID" class="ownline">Please select your primary category:*</label>
                <select name="categoryID" id="categoryID" class="required">
                    <option value="">Category 1</option>
                    <option value="">Category 2</option>
                    <option value="">Category 3</option>
                    <option value="">Category 4</option>
                    <option value="">Category 5</option>
                    <option value="">Category 6</option>
                    <option value="">Category 7</option>
                    <option value="">Category 8</option>
                    <option value="">and so on</option>
                </select>
            </div> 
        </div><!-- name -->
    </div>
    <div class="buttons"><input type="submit" value="Save and Go to Work History"/><input type="submit" value="Save"/></div>
</form>

Note that accordion is currently disabled - commented out completely.

And the relevant CSS:

label { float:left; text-align:right; width:30%; margin:0 14px 14px 0; line-height:20px; font-weight:bold; }
input, select, textarea { float:left; width:63%; margin-bottom:14px; background:#eee; line-height:1; }
    input[type=submit],  input[type=reset] { float:right; display:block; width:auto; margin:20px 0 0 14px; }
input:focus, textarea:focus { background:#fff; }
input, textarea { padding:2px 4px; }
select option { text-transform:capitalize; }
textarea { height:49px; }

.ownline { text-align:left; width:100%; line-height:20px; font-weight:bold; margin-bottom:3px; }
    .ownline+input,  .ownline+select,  .ownline+textarea, .ownline+.fieldinstructions+select { width:100%; }

label.error { color:#8e082d; }
    label.error:before { content: "\00bb\00a0"; display:inline-block; text-indent:-10px; }
input.error { border:2px solid #8e082d; background-color:#fdf5f3; } 

When I submit the form (with no filled out fields so that errors are generated) The class ownline on the categoryID label is removed and replaced by the error class. At least it seems to be the case. When I view the pages' source, the categoryID label looks like: below. (I removed the select options for brevity.)

<label for="categoryID" class="ownline">Please select your primary category:*</label>
<select name="categoryID" id="categoryID" class="required">
</select>

But in Firebug it looks like:

<label class="error" for="categoryID">Please select your primary category:*</label>
<select id="categoryID" class="required error" name="categoryID">
</select>

Interestingly, JQuery overwrote the existing class in the label, but appended to the existing class in the select.

In case this was a CSS specificity problem, I did test writing the ownline styles with label.ownline as the selector, but it didn't solve it.

This is a WordPress site with Contact Form 7 installed, but I added a function to functions.php so Contact Form wouldn't call its stuff on the page in question.

I've been Googling this for hours.

I'm definitely a JQuery noob. I'd love some help, but please speak slowly. :)

Thank you thank you,
Kim

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

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

发布评论

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

评论(1

扭转时空 2024-12-11 21:58:20
$(document).ready(function(){
    $("#profile-basic").validate({
        debug:true,
        errorElement: "nothing", // <-- this works !! (or "div", or something else)
        highlight: function(element, errorClass, validClass) {
            $(element).addClass(errorClass).removeClass(validClass);
            $(element.form).find("label[for=" + element.id + "]")
            .addClass(errorClass);
        },
        unhighlight: function(element, errorClass, validClass) {
            $(element).removeClass(errorClass).addClass(validClass);
            $(element.form).find("label[for=" + element.id + "]")
            .removeClass(errorClass);
        }
    });
});

我确信;)

要点是,验证插件有自己的错误管理方法,默认情况下它使用它找到的最近的“标签”,在这里我们告诉这个插件模块在其他地方寻找它的东西。

$(document).ready(function(){
    $("#profile-basic").validate({
        debug:true,
        errorElement: "nothing", // <-- this works !! (or "div", or something else)
        highlight: function(element, errorClass, validClass) {
            $(element).addClass(errorClass).removeClass(validClass);
            $(element.form).find("label[for=" + element.id + "]")
            .addClass(errorClass);
        },
        unhighlight: function(element, errorClass, validClass) {
            $(element).removeClass(errorClass).addClass(validClass);
            $(element.form).find("label[for=" + element.id + "]")
            .removeClass(errorClass);
        }
    });
});

and I'm sure ;)

point is, the validate plugin has is own error management method, and by default it uses the nearest "label" it finds, here we tell this plugin module to look somewhere else to do it's stuff.

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