【发布时间】:2021-12-15 13:21:12
【问题描述】:
对不起,如果我拼错了什么或使用了错误的词,在 JS 还是很新的。 因此,我在 HTML 中创建了两个不同的按钮,每个按钮都使用单独的对象列表访问各自的功能。下面是对象列表外观的一个小例子。 我正在尝试创建一个函数,它允许用户能够在输入搜索栏中搜索单词。但是我无法创建此功能以使其对两个按钮都有效,而无需在搜索栏中添加单词之前单击按钮。所以基本上如果搜索词匹配,它应该能够从两个列表中获取选项。
这甚至可能吗,还是我必须调整我已经必须能够制作这样的功能的功能,还是? (注意示例不包含搜索功能的示例,只是因为我什至不知道从哪里开始) 非常感谢您为我提供的任何帮助。
const liverpoolPlayers = [
{
name: "Mohamed Salah",
age: 27,
fromCountry: "Egypt",
position: "Angrep",
},
{
name: "Sadio Mane",
age: 25,
fromCountry: "Senegal",
position: "Angrep"
},
const playersContent = document.querySelector(".product-content")
const playersBtn = document.querySelector("#players-btn")
let showPlayers = () => {
playersContent.innerHTML = ""
liverpoolPlayers.forEach(player =>{
playersContent.innerHTML += `
<article class="product-article">
<h3>Name:${player.name}</h3>
<p>Age: ${player.age}</p>
<p>Country: ${player.fromCountry}</p>
<p>Postition: ${player.position}</p>
<img ${player.img}>
</article>
`
})
}
playersBtn.addEventListener("click", showPlayers)
const wineBtn = document.querySelector("#wine-btn")
let showWines = () => {
playersContent.innerHTML = ""
wineList.forEach (wine => {
playersContent.innerHTML += `
<article class="product-article">
<h3>Name:${wine.name}</h3>
<p>Age: ${wine.age}</p>
<p>Country: ${wine.fromCountry}</p>
<p>Postition: ${wine.position}</p>
</article>
`
})
}
wineBtn.addEventListener("click", showWines)
HTML 搜索输入
<section>
<label>Filtrer etter søkeord</label>
<input id="search-input">
<button id="search-btn">Søk</button>
</section>
HTML 按钮
<primary-button id="players-btn" text="Country"></primary-button>
HTML 按钮
【问题讨论】:
-
ID 为
wine-btn和players-btn的 HTML 元素在哪里? -
现在将按钮添加到问题中
-
由于某种原因它不会添加葡萄酒按钮,但它与国家按钮相同,只有 id i wine-btn 和 text = wine
-
我仍然无法重现该问题。可能是因为,没有
liverpoolPlayers和wineList数组。 -
你应该添加minimal reproducible code,这样人们可以清楚地了解你的问题。
标签: javascript function object input searchbar