[英]Ignore case-sensitive in search javascript
我有這樣的事情:
HTML
<input type="text" name="keyword" onkeyup="searchFunction()" style="width:300px;" />
Javscript
function searchFunction() {
$(".contact-name") /*Contact-name is the name of the div*/
.hide()
.filter(":contains('" + $("input[name='keyword']").val() + "')")
.show();
}
這樣就可以了,但是當div包含george之類的文本並且我在搜索文本框中鍵入GEORGE時,它不起作用,那么您可以幫我嗎?
filter
接受一個函數,所以我可能會使用它並進行不區分大小寫的比較:
function searchFunction() {
var find = $("input[name='keyword']").val().toLowerCase();
$(".contact-name") /*Contact-name is the name of the div*/
.hide()
.filter(function() {
return $(this).text().toLowerCase().indexOf(find) != -1;
})
.show();
}
我也很想不要將它們全部隱藏起來,然后顯示匹配項,而是分別更新每個匹配項:
function searchFunction() {
var find = $("input[name='keyword']").val().toLowerCase();
$(".contact-name") /*Contact-name is the name of the div*/
.each(function() {
var $this = $(this);
$this.toggle($this.text().toLowerCase().indexOf(find) != -1);
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.