我有一个问题
点击按钮Filter
我希望获得所选城市的条目
所以我禁止写大代码,所以我只显示数据div。如果你愿意,我想你可以添加选项和选项
(Select id = "cityFilter" , options: Dnepr, Kharkiv, Lviv)
HTML :
<div id="data">
<div>
<a class="userEmail" href="#">user1@domain.ua </a>
<span> user1 </span>
<span> Dnepr </span>
<span><input type = "button" value = "x" /></span>
</div>
<div>
<a class="userEmail" href="#">user2@domain.ua </a>
<span> user2 </span>
<span> Kharkiv </span>
<span><input type = "button" value = "x" /></span>
</div>
<div>
<a class="userEmail" href="#"> user3@domain.ua </a>
<span> user3 </span>
<span> Lviv </span>
<span><input type = "button" value = "x" /></span>
</div>
<div>
<a class="userEmail" href="#"> user4@domain.ua </a>
<span> user4 </span>
<span> Dnepr </span>
<span><input type = "button" value = "x" /></span>
</div>
<div>
<a class="userEmail" href="#"> user5@domain.ua </a>
<span> user5 </span>
<span> Lviv </span>
<span><input type = "button" value = "x" /></span>
</div>
</div>
cityFilter.js (评论中的代码说明):
$(function() {
//click on button
$('body > input').on('click', function() {
//get selected value
var cityFilter = $('#cityFilter').val();
//sort out spans which contains city names
$('#data > div > a + span + span').each(function() {
//for check the received value
console.log($(this).text());
//compare selected value and span text
if ($(this).text() != cityFilter) {
console.log('true');
}
});
});
});
我得到了所有true
。问题在哪里?
答案 0 :(得分:2)
你的跨度有前导和尾随空格
即。 " abc " != "abc"
请尝试以下方法:
if ($(this).text().trim() != cityFilter) {
console.log('true');
}