jQuery如何.find()不区分大小写? [英] JQuery how to .find() case insensitive?

查看:78
本文介绍了jQuery如何.find()不区分大小写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<Manufacturers>
  <Manufacturer name="abc">
    <flags=""/>
  </Manufacturer><Manufacturer name="abcd">
    <flags=""/>
  </Manufacturer>
  <Manufacturer name="abcde">
    <flags=""/>
  </Manufacturer>
<Manufacturers>

我想打印出名称中包含字符串'bc'的制造商的名称

I want to print out the names of just the manufacturers which contain the string 'bc' in the name

这是我到目前为止的尝试

This is my attempt so far

      $( "#autocomplete" ).on( "filterablebeforefilter", function ( e, data ) {
        var $ul = $(this);
        html = "";
        $ul.html( "<li><div class='ui-loader'><span class='ui-icon ui-icon-loading'></span></div></li>" );
        $ul.listview( "refresh" );
        $.ajax({
                type: "GET",
                url: "./Sources.xml",
                datatype: "xml",
                error: function(jqXHR, textStatus, errorThrown) {
                    console.log('Error: ' + errorThrown);
                },
                success: function(xml.toLowerCase()) {
                    console.log('AJAX Request is succeded.');

                    $(xml).find('Manufacturer[name*="' + $(data.input).val() + '"]').each(function(){
                        console.log($(this).attr('name'));
                    });
                    $ul.html(html);
                    $ul.listview( "refresh" );
                    $ul.trigger( "updatelayout");
                }
            });

    });

问题是.find()区分大小写。我怎样才能做同样但不区分大小写?

The problem is that .find() is case sensitive. How can I do the same but case insensitive

推荐答案

您应该匹配制造商,而不是对您的查找进行检查。 ,然后按制造商的名称搜索,并在函数中转换为小写的查询

Rather than building your check into your find, you should match off of "Manufacturer", and then search by the name of the manufacturer and the query both cast to lower case within the function

$(xml).find("Manufacturer").each(function(i){
  console.log($(this)[0].attributes.name.value.toLowerCase().indexOf(query.toLowerCase()) >= 0;
})

这篇关于jQuery如何.find()不区分大小写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆