2013-10-19 94 views
32

我做以下使用屬性包含選擇$('[attribute*=value]')不區分大小寫的jQuery屬性選擇

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

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

這適用於第一次輸入,但不是第二個輸入爲「」有資金「中號

如何讓$("input[name*='man']")不區分大小寫的選擇器?

+1

[CSS選擇器情況下可能重複對屬性不敏感](http://stackoverflow.com/questions/5671238/css-selector-case-insensitive-for-attributes) – falinsky

+0

[使用Jquery的不區分大小寫的屬性值選擇器](http:// stackoverflo wap.questions/5755722/case-insensitive-attribute-value-selector-with-jquery) –

回答

31

您可以隨時使用.filter()

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

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

這裏的關鍵部分是其toLowerCase()的小寫屬性name,使您可以測試它含有man

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

* REGEX_VALUE * - 值要查找

我結束了使用正則表達式來驗證屬性「ID」是否滿足...如果你想找到一個特定的正則表達式是更加靈活匹配一個或多個值,區分大小寫不敏感或或一定範圍的值...

1

我只是能夠完全忽略jQuery的情況下sensetivity達到我想要使用下面的代碼是什麼,

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

你可以使用這個鏈接根據您的jQuery的版本找到的代碼, https://css-tricks.com/snippets/jquery/make-jquery-contains-case-insensitive/

也有這篇文章的地方確實很多好東西用jQuery:​​

36

要做到這一點最簡單的方法是添加不區分大小寫標誌的「i」選擇的正則表達式部分內:

所以不是

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

你可以做

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

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

+6

FYI:適用於Chrome 53,但不適用於IE 11,如此有趣,但並不理想 – Stan

+0

@Stan這讓我感動。我在Chrome和IE11中看到了不同的結果,並認爲這不可能;還有一些其他的區別。然後我看到了你的評論。我認爲jQuery的重點是瀏覽器獨立性。我特別期待在選擇器語法這樣的內部結構時這是真實的。任何想法爲什麼這種差異存在或如果/它在哪裏記錄? – BlueMonkMN

+0

@BlueMonkMN它已經過了一年,我沒有重新測試,但我的猜測是它是IE瀏覽器的正則表達式的處理是責怪的差異。 – Stan

0

這工作我使用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; 
    } 
相關問題