我正在一个可以搜索音乐的网站上工作。我想在一个过滤器选项中,人们可以过滤最小年龄。
这是我的JSON文件:
{
"artists":[
{
"name": "Martin Garrix",
"origin": "Netherlands",
"age": "22 years old",
"artist_id": "c0dc129d8a886a2c9bf487b826c3614a6630fb9c",
"best_song":"https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/98081145&color=%23ff5500&auto_play=false&hide_related=false&show_comments=true&show_user=true&show_reposts=false&show_teaser=true"
},
{
"name": "Armin van Buuren",
"origin": "Netherlands",
"age": "41 years old",
"artist_id": "8b0a178ce06055312456cccc0c9aa7679d8054f1",
"best_song":"https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/181749728&color=%23ff5500&auto_play=false&hide_related=false&show_comments=true&show_user=true&show_reposts=false&show_teaser=true"
},
{
"name": "Don Diablo",
"origin": "Netherlands",
"age": "38 years old",
"artist_id": "178c6e7e3bf24710d4895048586a86f1bb81d842",
"best_song":"https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/212802517&color=%23ff5500&auto_play=false&hide_related=false&show_comments=true&show_user=true&show_reposts=false&show_teaser=true"
},
{
"name": "David Guetta",
"origin": "France",
"age": "50 years old",
"artist_id": "c2714423228a8012ad37af0186447cbb7ff589f7",
"best_song":"https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/140006470&color=%23ff5500&auto_play=false&hide_related=false&show_comments=true&show_user=true&show_reposts=false&show_teaser=true"
},
{
"name": "Alesso",
"origin": "Sweden",
"age": "26 years old",
"artist_id": "69fa09f6e181093fe0ea2ce1a4099066509f7837",
"best_song":"https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/288914798&color=%23ff5500&auto_play=false&hide_related=false&show_comments=true&show_user=true&show_reposts=false&show_teaser=true"
}
]
}
这是我的HTML:
<div class = "div-age">
<label for="age-select">Minimum age</label>
<input type="text" class = "search-age">
</div>
所以例如;如果你在输入字段中输入40,那么你只能得到#Armin van Buuren&#34;和#34; David Guetta&#34;。
这就是我现在所拥有的。
document.querySelector(`.search-age`).addEventListener(`keyup` , e => {
const term = e.target.value.toLowerCase();
const ageList = document.getElementsByClassName(`artist-info`);
Array.from(ageList).forEach(function(ageList) {
const artistAge = ageList.querySelector(`.age`).textContent;
if (artistAge.toLowerCase().includes(term)) {
ageList.style.display = `flex`;
} else {
ageList.style.display = `none`;
}
})
});
我现在遇到的问题是它只过滤确切的数字。例如,我把22,我只得到&#34; Martin Garrix&#34;
答案 0 :(得分:1)
答案 1 :(得分:0)
您可以尝试使用parseInt
将年龄字符串转换为相应的数字,然后将其与值进行比较:
// Convert term and artist's age to corresponding integers
term = parseInt(term, 10);
artistAge = parseInt(artistAge, 10);
if (artistAge >= term) {
ageList.style.display = `flex`;
} else {
// ...
}
答案 2 :(得分:0)
有几件事:
你不需要Template literals,你可以使用普通字符串。
不要使用keyup
,而是使用input作为此类事件的事件:如果用户使用与键盘不同的设备粘贴值,它也可以获取内容 - 例如鼠标。
不要使用getElementsByClassName,因为它会返回实时HTMLCollection。请改用querySelectorAll,返回静态NodeList。它还直接支持forEach,因此您无需使用Array.from
。
不要shadowing变量,例如您编写的代码中的ageList
:它可能会导致意外错误。
说:
// you might not want to query all the time this list,
// every time the user change the input value.
const ageList = document.querySelectorAll('.artist-info');
document.querySelector('.search-age').addEventListener('input' , e => {
// no need to lowercase, it's numeric.
// you might want to add some check or constraint on
// <input> validation
const term = +e.target.value;
ageList.forEach(node => {
const artistAge = node.querySelector('.age').textContent;
node.style.display = parseInt(artistAge, 10) >= term ? 'flex' : 'none';
})
});