简体   繁体   中英

Cannot use has-error in some input element

I'm trying to validate some inputs available in a modal:

<div id="modal-section" class="modal fade bd-example-modal-lg" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog modal-lg">
    <div class="modal-content">
        <div class="modal-header">
        <h3 class="modal-title">Manage</h3>
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
            <i class="tim-icons icon-simple-remove"></i>
        </button>
        </div>
        <div class="modal-body">
        <div class="modal-message alert" style="display:none"></div>
        <form>
            <fieldset>
                <legend>Details</legend>
                <div class="row">
                    <div class="col-xs-12 col-sm-6">
                        <div class="form-group">
                            <label class="control-label">name</label>
                            <input type="text" class="required form-control black-content">
                        </div>
                    </div>
                </div>
            </fieldset>

            <fieldset>
                <legend>
                    Informations
                </legend>

                <div id="questions-container">
                    <div class="card" data-id="1">
                        <div class="card-body">
                            <div class="row">
                                <div class="col-xs-12 col-sm-8">
                                    <div class="form-group">
                                        <label for="question-name" class="control-label">description *</label>
                                        <input type="text" class="required form-control" data-id="1">
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </fieldset>
        </form>
    </div>
        <div class="modal-footer">
        <button type="button" id="validate" class="btn btn-primary">save</button>
        <button type="button" class="btn btn-secondary" data-dismiss="modal">close</button>
        </div>
    </div>
</div>  

When I press the button validate this function will fire:

var missingRequired = false;

$('#modal-section .required').each(function () {
    $(this).closest('.form-group').removeClass('has-error');

    if ($(this).val() == '' || $(this).val() == undefined) {
        $(this).closest('.form-group').addClass('has-error');
        missingRequired = true;
    }
});

Essentially, this will check all the input that have the required class; only the input name will have the red, the other inputs available in the questions-container will not.

If I look at the HTML I can see that the class has-error is added also to the input available in questions-container but the inputs aren't colored. The CSS available on the element I have:

theme class:

.white-content .card:not(.card-white) label:not(.btn) {
    color: #344675;
}

bootstrap class (commented in the question-container element)

.has-error .form-control-feedback, .has-error .control-label {
    color: #ec250d;
}

Why is this happening?

From what i can tell with the information provided, your error CSS rule is not specific enough. Try changing it to:

.card .form-group.has-error label.control-label {
    color: #ec250d;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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