简体   繁体   English

查找特定标签的父项

[英]Find parent of specific label

I'm using jQuery validator for a questionaire. 我正在使用jQuery验证程序进行问卷调查。 I want to show the error message of a certain label behind the question (.vraag), but I can't get it to work. 我想在问题(.vraag)后面显示某个标签的错误消息,但是无法正常工作。

How do I append the error to the parrent (with class vraag) of this .find( "label[for='" + element.attr( "id" ) + "']" ) 我如何将错误附加到此.find(“ label [for ='” + element.attr(“ id”)+“']”的父母(带有vraag类)

This is the HTML: 这是HTML:

<div><p class="vraag"><strong>Heb je behoefte aan meer contact met ?</strong></p>  


<label for="antwoord1"><input id="antwoord1" class="antwoord1" type="radio" name="antwoord1" value="Ja, ik vind het leuk om  te ontmoeten"  > Ja, ik vind het leuk om te ontmoeten</label><br>

<label><input class="antwoord1" type="radio" name="antwoord1" value="Ja maar alleen per mail" > Ja maar alleen per mail</input></label><br>

<label><input class="antwoord1" type="radio" name="antwoord1" value="Ja maar alleen telefonisch"  > Ja maar alleen telefonisch</label><br>

<label><input id="contactvoldoende" type="radio" name="antwoord1" value="Nee, ik vind het contact voldoende" > Nee, ik vind het contact voldoende <span class="contactvoldoendetekst">(je hoeft de rest van de vragen niet in te vullen)</span></label><br>

</div>

This is the jQuery: 这是jQuery:

// validate the form when it is submitted
    var validator = $("#enquete").validate({
        errorPlacement: function(error, element) {
            // Append error within linked label
            $( element )
                .closest( "form" )
                    .find( "label[for='" + element.attr( "id" ) + "']" )
                        .append(error);
        },
        errorElement: "span",
            });


jQuery.extend(jQuery.validator.messages, {
    required: " (geef een antwoord)"
});
//Add following scripts to your HTML file in head section

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.17.0/jquery.validate.min.js"></script>

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

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