【问题标题】:I want to get the value of ng-show through jq, but it is not the result I expected我想通过jq获取ng-show的值,但不是我预期的结果
【发布时间】:2019-05-21 21:36:58
【问题描述】:

我可以打印出所有的 ng-show 值,但我无法过滤出我想要的。
不知道是正则表达式错误还是angularjs。我直接在开发者工具里试了一下,结果成功了。
请查看codepen中的完整代码,谢谢

var thList = $('thead').find('th');
        var fieldPattern = /\'(.*)\'.*\|.*/g;
        var allTableField = [];
        var valueList = [];
        angular.forEach(thList, function(th) {
            var value = $(th).attr('ng-show');
            console.log(value);
            if (/showField/g.test(value)) {
                valueList.push(value);
                var results = fieldPattern.exec(value);
                if (results) {
                    allTableField.push(results[1]);
                }
            }
        });

Codepen

对不起,我的英语很糟糕,很难表达。
我想通过 JQ 获取 ng-show 值并过滤掉与正则表达式匹配的值。
最后,我只得到了一部分。

enter image description here

【问题讨论】:

  • 嘿,你能改进一下你的问题吗?目前尚不清楚您要完成什么
  • 我试着更清楚地描述它。我希望你能理解我的问题。非常感谢。

标签: javascript angularjs


【解决方案1】:

因此,您的 valueList 包含的“值”比您的 allTabField 多,其余值由您的正则表达式 fieldPattern 过滤

编辑:

使用像var fieldPattern = new RegExp("\'(.*)\'.*\|.*");这样的RegExp构造函数

替代解决方案:

angular.forEach(thList, function(th) {
                    var value = $(th).attr('ng-show');
                    console.log(value);
                    if (/showField/g.test(value)) {
                        valueList.push(value);
                        /* changes are made here */
                        var index = value.indexOf('|');
                        var results = value.substring(1, index - 1);
                        if (results) {
                            allTableField.push(results);
                        }
                        /* End of changes */
                    }
                });

【讨论】:

  • 非常感谢您的回答。我使用正则表达式是因为ng-show 值中可能有空格
  • @Scheinin 欢迎您,我编辑了我的答案,这可能对您有用
  • 谢谢。new RegExp("\'(.*)\'.*\|.*") 为我工作。但我找不到new RegExp("\'(.*)\'.*\|.*")/\'(.*)\'.*\|.*/g 之间的区别。
  • @Scheinin stackoverflow.com/questions/6149770/… 检查这个问题可能会有所帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多