不区分大小写的 jQuery 属性选择器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19465157/
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-27 00:02:15  来源:igfitidea点击:

Case insensitive jQuery attribute selector

javascriptjqueryjquery-selectors

提问by gshaffer

I am doing the following using attribute contains selector $('[attribute*=value]')

我正在使用属性包含选择器$('[attribute*=value]')

<input name="man-news">
<input name="milkMan">

<script>    
    $( "input[name*='man']").css("background-color:black");
</script>

This works for the 1st input but not the second input as "Man" has a capital "M"

这适用于第一个输入,但不适用于第二个输入,因为“ Man”有一个大写的“ M

How can I make $( "input[name*='man']")an case insensitive selector?

如何制作$( "input[name*='man']")不区分大小写的选择器?

采纳答案by Bojangles

You can always use .filter():

您可以随时使用.filter()

var mans = $('input').filter(function() {
    return $(this).attr('name').toLowerCase().indexOf('man') > -1;
});

mans.css('background-color', 'black');

The key part here is toLowerCase()which lowercases the nameattribute, allowing you to test it for containing man.

这里的关键部分是toLowerCase()小写name属性,允许您测试它是否包含man.

回答by araneae

The simplest way to do this is to add a case insensitivity flag 'i' inside the regex part of the selector:

最简单的方法是在选择器的正则表达式部分添加一个不区分大小写的标志“i”:

So instead of

所以代替

$( "input[name*='man']")

$( "input[name*='man']")

You could do

你可以做

$( "input[name*='man' i]")

$( "input[name*='man' i]")

JS fiddle: https://jsfiddle.net/uoxvwxd1/3/

JS小提琴:https: //jsfiddle.net/uoxvwxd1/3/

回答by Juvil

var control = $('input').filter(function() {
    return /*REGEX_VALUE*/i.test($(this).attr('id'));
});

*REGEX_VALUE* - the value you want to find

*REGEX_VALUE* - 您要查找的值

I ended up using regex to validate whether the attribute 'ID' satisfy... regex is much more flexible if you want to find a certain matching value or values, case sensitive or insensitive or a certain range of values...

我最终使用正则表达式来验证属性“ID”是否满足......如果你想找到一个或多个匹配的值,区分大小写或不敏感或特定范围的值,正则表达式更加灵活......

回答by Umesh Patil

I was just able to ignore jQuery's case sensitivity altogether to achieve what I want using below code,

我只是能够完全忽略 jQuery 的区分大小写,以使用下面的代码实现我想要的,

$.expr[":"].contains = $.expr.createPseudo(function(arg) {
  return function( elem ) {
    return $(elem).text().toUpperCase().indexOf(arg.toUpperCase()) >= 0;
  };
});

You can use this link to find code based on your jQuery versions, https://css-tricks.com/snippets/jquery/make-jquery-contains-case-insensitive/

您可以使用此链接根据您的 jQuery 版本查找代码, https://css-tricks.com/snippets/jquery/make-jquery-contains-case-insensitive/

Also there is an article where it does to many good things with jQuery: http://www.ultechspot.com/jquery/using-jquery-search-html-text-and-show-or-hide-accordingly

还有一篇文章用 jQuery 做了很多好事:http: //www.ultechspot.com/jquery/using-jquery-search-html-text-and-show-or-hide-accordingly

回答by S.shaikh

This works for me using jQuery and if i'm adding item to a table

这对我使用 jQuery 有效,如果我将项目添加到表格中

    // check if item already exists in table
    var inputValue = $('#input').val(); // input
    var checkitem = $('#exampleTable td.value div.editable').filter(function() {

        //check each table's editable div matches the input value in lowercase 
        if ($(this).text().toLowerCase() === inputValue.toLowerCase()) {
            itemexists = true; 
        }   
    });

    if (itemexists) {
        alert("item exists in the table");
        return;
    }