jQuery如何.find()不区分大小写?

          

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

这是我到目前为止的尝试

  $( "#autocomplete" ).on( "filterablebeforefilter", function ( e, data ) { var $ul = $(this); html = ""; $ul.html( "
  • " ); $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()区分大小写。 我怎么能这样做但不区分大小写

    您应该匹配“制造商”,然后搜索制造商的名称和查询都转换为函数中的小写,而不是将您的检查构建到您的查找中。

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

    我建议如下:

     var xml = ''; // we create a jQuery objecct from the xml string (above), // find all the Manufacturer elements, // filter those elements using the filter() method: var bcInName = $(xml).find('Manufacturer').filter(function() { // keeping only those elements in which the lower-cased 'name' // attribute contains the string 'bc': return this.attributes["name"].value.toLowerCase().indexOf('bc') > -1; // using map() to create a map: }).map(function() { // consisting of the value of the name attribute from the // elements that we kept in the collection: return this.attributes["name"].value; // using get() to convert the map to an array: }).get(); // using this to write a log in the 'result' pane of // of the snippet: snippet.log(bcInName); // logging to the console: console.log(bcInName); 
     var xml = ''; var bcInName = $(xml).find('Manufacturer').filter(function() { return this.attributes["name"].value.toLowerCase().indexOf('bc') > -1; }).map(function() { return this.attributes["name"].value; }).get(); snippet.log(bcInName); console.log(bcInName); 
     p::before { content: 'Names found: '; color: #666; } 
        

    根据这个jQuery问题 , children()函数区分大小写,因此您可以使用它( 演示 ):

     var xml = '', $results = $( xml ).children( 'Manufacturer[name*="' + $(data.input).val() + '"]' ); $( "#result" ).append( '# results = ' + $results.length );