【问题标题】:"label.error" seems to always be present on every bootstrap tab“label.error”似乎总是出现在每个引导选项卡上
【发布时间】:2015-05-04 18:25:44
【问题描述】:

我不明白为什么$('label.error') 会显示在每个引导选项卡上,而该特定元素应该只显示在一个选项卡上。我有一个字段未通过引导选项卡上的验证,因此在违规字段上附加了带有 class 错误的 label。但是,我似乎无法让我的代码登陆具有违规字段的特定选项卡。我在下面的代码中做错了什么? isErrorPresent 应该只在 1 个特定选项卡上为真,但每个选项卡都显示它为真...

$("#" + formId).validate({ignore:""}).form(); // ignore:"" allows for hidden fields to be validated as well

    $(".tab-content").find("div.tab-pane").each(function (index, tab) {
        var id = $(tab).attr("id");
        $('a[href="#' + id + '"]').click();

        alert('processing tab ' + id);

        var isErrorPresent = $('div.tab-pane, div.active').find('label.error').length > 0;

        alert(isErrorPresent);

//        if (isErrorPresent) {
//            alert("label.error detected...");
//            hasError = true;
//            return false; // Break .each loop and return to page
//        }
    });

【问题讨论】:

    标签: javascript jquery twitter-bootstrap tabs


    【解决方案1】:

    没有看到更多的标记,这就是我的想法:

    你正在这样做:

    var isErrorPresent = $('div.tab-pane, div.active').find('label.error').length > 0;
    

    选择器中有一个逗号,表示您要检查 div.tab-pane && div.active

    这就是你想要的吗?也许你打算这样做(没有逗号和空格):

    var isErrorPresent = $('div.tab-pane.active').find('label.error').length > 0;
    

    【讨论】:

    • 一个活动的引导选项卡具有这两个类...html 看起来像 <div class=tab-pane active>
    【解决方案2】:

    在@Red2678 的帮助下解决了这个问题(谢谢)...这就是现在对我有用的东西...

        // Check all tabs for errors & show first tab that has errors
        $(".tab-content").find("div.tab-pane").each(function (index, tab) {
            var id = $(tab).attr("id");
            $('a[href="#' + id + '"]').click();
    
            $("#" + formId).validate().form();
    
            var activeTab = $('div.tab-pane.active');
    
            var isErrorPresent = false;
            if ($(activeTab).find('label.error').length > 0) {
                isErrorPresent = $(activeTab).find('label.error').css('display') !== 'none';
            }
    
            if (isErrorPresent) {
                hasError = true;
                return false; // Break .each loop and return to page
            }
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-22
      • 2023-03-27
      • 2011-05-09
      • 1970-01-01
      • 2018-12-23
      • 2013-10-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多