Javascript 如何使用正则表达式通过 jQuery 通过 ID 选择元素?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13541898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 12:19:10  来源:igfitidea点击:

How can I select an element by ID with jQuery using regex?

javascriptjqueryregex

提问by Alexander

I have the following input elements:

我有以下输入元素:

<input id="AAA_RandomString_BBB" type="text" />
<input id="AAA_RandomString_BBB_Start" type="text" />
<input id="AAA_RandomString_BBB_End" type="text" />

AAA & BBB are constants and I will always know what they are. However RandomString will always be random.

AAA 和 BBB 是常数,我将永远知道它们是什么。但是 RandomString 将始终是随机的。

I want to get the value of AAA_RandomString_BBB. I do not want the values from the input elements with ID ending in either _Start or _End.

我想获取 AAA_RandomString_BBB 的值。我不想要 ID 以 _Start 或 _End 结尾的输入元素的值。

I tried the folowing:

我尝试了以下内容:

$('[id^="AAA_"]')

But the above selects all of the input elements that have an ID starting with "AAA_"

但是上面选择了所有ID以“AAA_”开头的输入元素

I need some way to select using a regex type syntax like:

我需要某种方式来选择使用正则表达式类型的语法,如:

$('[id^="AAA_(.+?)_BBB"]')

Is this possible? If not, can anyone suggest a method of selecting

这可能吗?如果没有,任何人都可以建议一种选择方法

回答by Alexander

You can combine both selectors in a multiple attribute selector.

您可以在多属性选择器中组合两个选择

?$("[id^=AAA_][id$=_BBB]")

It will return all the elements that matches all the specified attribute filters:

它将返回与所有指定属性过滤器匹配的所有元素:

  • [id^=AAA_]matches elements with idattribute starting with AAA_, and
  • [id$=_BBB]matches elements with idattribute ending with _BBB.


Another generic alternatives:

另一个通用替代方案:

回答by Praveen Kumar Purushothaman

Use this:

用这个:

$('[id^="AAA_"][id$="_BBB"]')

Fiddle: http://jsfiddle.net/J6hGx/

小提琴:http: //jsfiddle.net/J6hGx/

回答by vvohra87

You can look for id's starting with AAAand ending with BBBlike this:

您可以像这样查找 id 的开头AAA和结尾BBB

?$("[id^=AAA_][id$=_BBB]")

The working fiddle: http://jsfiddle.net/DN9uV/

工作小提琴:http: //jsfiddle.net/DN9uV/

回答by beipawel

This can be done like so:

这可以像这样完成:

$('input').filter(function(){ return this.id.match(/^AAA_.+_BBB$/) })

You can give use $('input', <context>)to make the search more precise. See also here

您可以使用$('input', <context>)使搜索更精确。另见此处

回答by Huzaifa Qamer

It would be better to just check for the id ending on _BBB by

最好通过以下方式检查以 _BBB 结尾的 ID

 $("[id$=_BBB]")

回答by Bimal Das

How about this :

这个怎么样 :

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
    </head>
    <body>
        <input id="AAA_RandomString_BBB" type="text" />
        <input id="AAA_RandomString_BBB_Start" type="text" />
        <input id="AAA_RandomString_BBB_End" type="text" />

        <script>
            $(document).ready(function () {
                debugger
                var targetElement = $('input')
                .filter(function () {
                    var el = this.id.match(/^AAA.*BBB$/g);
                    return el;
                });
                console.log(targetElement.val());
            })
        </script>
    </body>
</html>

回答by Michel Feldheim

I use to solve this with the class selectors which don't need to be unique.

我使用不需要唯一的类选择器来解决这个问题。

This also has a positive effect on speed because no complex search is required Additionally you can use this for the different styling of different elements

这也对速度有积极影响,因为不需要复杂的搜索此外,您可以将其用于不同元素的不同样式

e.g.

例如

<elem id="1" class="aa">element type aa</elem>
<elem id="2" class="aa">element type aa</elem>
<elem id="3" class="aa bb">element type aa and bb</elem>
<elem id="4" class="bb">element type bb</elem>

Now you can simply use the class selector

现在您可以简单地使用类选择器

$('.aa').click(function(){
     console.log( 'clicked type aa #' + $(this).attr('id') );
});

$('.bb').click(function(){
     console.log( 'clicked type bb #' + $(this).attr('id') );
});