<Manufacturers>
<Manufacturer name="abc">
<flags=""/>
</Manufacturer><Manufacturer name="abcd">
<flags=""/>
</Manufacturer>
<Manufacturer name="abcde">
<flags=""/>
</Manufacturer>
<Manufacturers>
我想打印出包含字符串&#39; bc&#39;的制造商的名字。在名称
这是我到目前为止的尝试
$( "#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()区分大小写。我怎样才能做同样但不区分大小写的
答案 0 :(得分:3)
您应该匹配“制造商”,然后搜索制造商的名称和查询,并将其转换为功能中的小写
$(xml).find("Manufacturer").each(function(i){
console.log($(this)[0].attributes.name.value.toLowerCase().indexOf(query.toLowerCase()) >= 0;
})
答案 1 :(得分:2)
我建议如下:
var xml = '<Manufacturers><Manufacturer name="abc"><flags=""/></Manufacturer><Manufacturer name="abcd"><flags=""/></Manufacturer><Manufacturer name="abcde"><flags=""/></Manufacturer><Manufacturers>';
// 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 = '<Manufacturers><Manufacturer name="abc"><flags=""/></Manufacturer><Manufacturer name="abcd"><flags=""/></Manufacturer><Manufacturer name="abcde"><flags=""/></Manufacturer><Manufacturers>';
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);
&#13;
p::before {
content: 'Names found: ';
color: #666;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
&#13;
外部JS Fiddle demo,用于实验。
顺便提一下,值得注意的是,在CSS的Selectors Level 4模块中,不区分大小写的属性选择器将 - 除非选择器在将来从模块中删除 - 通过以下表示法允许:
[frame=hsides i] {
border-style: solid none;
}
请注意hsides
和i
标识符之间的空格,该标识符将为:
...设置[带有] frame属性的元素,当它具有hsides值时,该值是否表示为
hsides
,HSIDES
,hSides
等等,即使在属性值区分大小写的XML环境。
参考文献:
答案 2 :(得分:0)
根据this jQuery issue,children()
函数区分大小写,因此您可以使用它(demo):
var xml = '<Manufacturers><Manufacturer name="abc"><flags=""/></Manufacturer><Manufacturer name="abcd"><flags=""/></Manufacturer><Manufacturer name="abcde"><flags=""/></Manufacturer></Manufacturers>',
$results = $( xml ).children( 'Manufacturer[name*="' + $(data.input).val() + '"]' );
$( "#result" ).append( '# results = ' + $results.length );