简体   繁体   English

jQuery父选择器查找步骤

[英]JQuery parent selector to find step

I am using a jquery steps control. 我正在使用一个jQuery步骤控件。 The form is posted in the onFinished() event with an Ajax request being fired off. 表单被发布在onFinished()事件中,并触发了Ajax请求。 From the server I return the following if any validation errors occur: 如果发生任何验证错误,我将从服务器返回以下内容:

{
    "ErrorMessages": 
        [{
            "PropertyName":"Complainant.Address.StreetName",
            "ErrorMessage":"The complainant street name is required."
        },
        {
            "PropertyName":"Complainant.Address.Suburb",
            "ErrorMessage":"The complainant suburb is required."
        }]
}

Next in jQuery I find the appropriate span element (that is created with the ASP.NET MVC's @Html.ValidationMessageFor ) and give it the provided validation message: 接下来在jQuery中,我找到合适的span元素(由ASP.NET MVC的@Html.ValidationMessageFor创建),并为其提供验证消息:

if (response.ErrorMessages != null) {
    for (var i = 0; i < response.ErrorMessages.length; i++) {
        var validationSpan = $("span[data-valmsg-for='" + response.ErrorMessages[i].PropertyName + "'");
        validationSpan.html(response.ErrorMessages[i].ErrorMessage);
        validationSpan.removeClass("field-validation-valid");
        validationSpan.addClass("field-validation-error");
    }
}

Next step is to set the Jquery step header as red in order to show that a validation error has occurred on that step. 下一步是将Jquery步骤标题设置为红色,以表明该步骤已发生验证错误。 This part is not working: 这部分不起作用:

var section = validationSpan.parent(".step-div");
$("#steps-uid-0-t-" + section.attr("step")).css("background", "#ff3111");

I have the following html structure: 我有以下html结构:

<div>
    <h1>Complaint details</h1>
    <section>
        <div class="step-div" step="0">
            <div class="form-group">
                @Html.LabelFor(model => model.Complainant.Address.StreetName, new { @class = "col-md-3 control-label" })
                <div class="col-md-8">
                    @Html.TextBoxFor(model => model.Complainant.Address.StreetName, new { @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.Complainant.Address.StreetName)
                </div>
            </div>
        </div>
    </section>
<div>

The part that seems to not be working: 似乎无法正常工作的部分:

var section = validationSpan.parent(".step-div");

This should give me the <div> that's directly inside the <section> , but it doesn't seem to. 这应该给我直接位于<section>内部的<div> <section> ,但似乎没有。 If I log this section variable I get the following: 如果我记录此section变量,则会得到以下信息:

{ length: 0, prevObject: Object, context: HTMLDocument ? 134 }

在此处输入图片说明

The rendered <a> element that I want to apply the style to looks like this (depending on which step it is): 我要应用样式的呈现的<a>元素看起来像这样(取决于它在哪一步):

Step 1: 第1步:

<a id="steps-uid-0-t-0" href="#steps-uid-0-h-0" aria-controls="steps-uid-0-p-0"><span class="number">1.</span> Complainant details</a>

Step 2: 第2步:

<a id="steps-uid-0-t-1" href="#steps-uid-0-h-1" aria-controls="steps-uid-0-p-1"><span class="number">2.</span> Complaint details</a>

Try .parents() instead of .parent() . 尝试使用.parents()而不是.parent() Better yet, try the .closest() . 更好的是,尝试.closest()

.parents() gets all the ancestors that match the given selector. .parents()获取与给定选择器匹配的所有祖先。

.parent() looks up only on the direct parent. .parent()仅在直接父级上查找。

.closest() looks up element itself and the ancestors to return the first one that matches the given selector. .closest()查找元素本身和祖先,以返回与给定选择器匹配的第一个元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM