私はいくつかのリストを持っています。ListA:item1、item2、…ListB:item1、item2、
アイテムをクリックすると、検索バーに値として表示されます。異なるリストのいくつかの項目を値として検索バーに追加する必要があります。現在のように、アイテムをクリックすると、現在の値が検索バーに置き換えられるだけです。
たとえば、検索バーに次のように表示できます。itemA1itemB2…
コードは次のとおりです。
var items = document.getElementById("index");
var item = items.getElementsByTagName("h3");
for(var i=0; i<item.length; i++) {
item[i].addEventListener("click", function(e) {
document.getElementById("search").value = (this.textContent || this.innerText);
recherche();
});
}
誰かがこれについて私を助けることができれば、それは大いにありがたいです!ありがとうございました
私はあなたがこのようなものが欲しいと思います。
var item = document.querySelectorAll("#index h3");
itemArr = [];
for(var i=0; i<item.length; i++) {
item[i].addEventListener("click", function(e) {
var itemVal = (this.textContent || this.innerText);
var index = itemArr.indexOf(itemVal);
if(index == -1){
itemArr.push(itemVal);
}else if (index > -1) {
itemArr.splice(index, 1);
}
document.querySelector("#search").value = itemArr.join(",");
});
}
function clearAll(){
itemArr = [];
document.querySelector("#search").value = "";
}
h3{
cursor:pointer;
}
.delete{
cursor:pointer;
color:red;
}
<input type="text" id="search"> <span class="delete" onclick="clearAll()">X</span>
<div id ="index">
<h3>item A1</h3>
<h3>item A2</h3>
<h3>item B1</h3>
<h3>item B2</h3>
</div>
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加