缺少jQuery日期选择器glyphicon图标

时间:2018-06-14 15:47:30

标签: javascript jquery jquery-validate

过去几天,我遇到了一个非常奇怪的&奇即可。我正在使用jQuery验证插件来验证我的字段,名为"开始日期" 。此字段有一个简单的验证方法,其中 required 设置为 true 。我能够成功验证该领域。但是,我收到了一个奇怪的问题。当字段验证为 有效或无效时, glyphicon-calendar 图标将会神奇地消失。我尝试通过查看检查器工具进行故障排除,结果验证了此代码,即压缩图标已完全删除

  <span class="glyphicon glyphicon-calendar"></span>

我花了几个小时研究,但似乎没有人接受这个奇怪的问题。有谁知道为什么? 非常需要帮助!以下是我的问题和我当前代码的截图

glyphicon-calender icon **missing** when field is **valid**

glyphicon-calender icon **missing** when field is **invalid**

glyphicon-calender span element **missing** by looking at **inspector tool**

 $(document).ready(function () {
        $.validator.setDefaults({
            highlight: function (element, errorClass, validClass) {
                if (element.type === "radio") {
                    this.findByName(element.name).addClass(errorClass).removeClass(validClass);
                } else {
                    $(element).closest('.form-group').removeClass('has-success').addClass('has-error has-feedback');
                    $(element).closest('.form-group').find('span.glyphicon').remove();
                    $(element).closest('.form-group').append('<span class="glyphicon glyphicon-remove form-control-feedback" aria-hidden="true"></span>');
                }
            },
            unhighlight: function (element, errorClass, validClass) {
                if (element.type === "radio") {
                    this.findByName(element.name).removeClass(errorClass).addClass(validClass);
                } else {
                    $(element).closest('.form-group').removeClass('has-error has-feedback').addClass('has-success has-feedback');
                    $(element).closest('.form-group').find('span.glyphicon').remove();
                    $(element).closest('.form-group').append('<span class="glyphicon glyphicon-ok form-control-feedback" aria-hidden="true"></span>');
                }
            }
        });

        $('#dataForm').validate({
            rules: {
                startDateInput: {
                    required: true
                }
            },
            submitHandler: function (form) {
                alert('success');
            }
        });
    });
<form id="dataForm" method="post" action="#">
<div class="form-group">
        <label class="control-label col-md-4" for="startDateInput">Start Date</label>
        <div class="input-group date" id="startDateInput" style="max-width: 280px" data-provide="datepicker">
            <input type="text" class="form-control" name="startDateInput">
            <div class="input-group-addon">
                <span class="glyphicon glyphicon-calendar"></span>
            </div>
        </div>
        <input type="submit" class="btn btn-primary" value="Save" id="saveButton" />
    </div>
</form>

演示:https://jsfiddle.net/Issaki1/gud7xjy0/10/

1 个答案:

答案 0 :(得分:0)

正如@Rich在评论中所提到的,以下行删除了图标。

$(element).closest('.form-group').find('span.glyphicon').remove();