因此,我正在研究一個帶搜索功能的網頁,用戶可以在其中鍵入事件名稱和朋友的姓名,並且該頁面將顯示基於這些輸入的搜索結果。打印出多個搜索結果
提前道歉,我的代碼是一個絕對亂七八糟的(但它的工作原理,這就是我現在所關心的)但是,我的問題是,我不知道打印出所有搜索結果的好方法。我看着在互聯網上,我有現在有以下幾點:
$('#searchFeedback').empty();
$.each(data.events[i], function (index, value) {
$('#searchFeedback').append(output);
})
這是我找到的最好的選擇(也是唯一以任何方式工作的一個,即使我在JQuery的混合),但問題是它會重複打印出最後的搜索結果,而不是打印出每個搜索結果。我對JQuery完全無能爲力。
這裏是我的整個JS代碼:
<script>
function eventSearch() {
var eName = document.getElementById("ename").value;
var fName = document.getElementById("fname").value;
var searchResults = "";
var friendResults = "";
var attendeeText = "";
if (eName === "" && fName === "") {
document.getElementById("searchFeedback").innerHTML = "<h4 class='eName'>Fields cannot be empty!</h4>";
} else {
for (var i = 0; i < data.events.length; i++) {
searchResults = data.events[i].eName.toUpperCase();
var output = "<h4 class='eName'>" + data.events[i].eName + " • " + data.events[i].date + "</h4> <p class='eDesc'>" + data.events[i].description + "<br><sub>" + attendeeText + "<a class='event' onclick='openEventLink(" + i + ");' href=''><b> Check it out here.</b> </a></sub></p><br>";
if (eName != "" && searchResults.includes(eName.toUpperCase())) {
if (data.events[i].attendees.length != 1) {
for (var a = 0; a < data.events[i].attendees.length; a++) {
attendeeText = data.events[i].attendees.join(", ");
}
} else {
attendeeText = data.events[i].attendees;
}
if (data.events[i].attendees.length > 1) {
attendeeText = attendeeText + " are going.";
} else if (data.events[i].attendees.length == 1) {
attendeeText = attendeeText + " is going.";
} else {
attendeeText = "";
}
if (fName != "") {
for (var a = 0; a < data.events[i].attendees.length; a++) {
friendResults = data.events[i].attendees[a].toUpperCase();
if (friendResults.includes(fName.toUpperCase())) {
console.log(searchResults);
console.log(friendResults);
$('#searchFeedback').empty();
$.each(data.events[i], function (index, value) {
$('#searchFeedback').append(output);
})
}
}
} else {
// Print results
}
} else if (eName == "" && fName != "") {
for (var a = 0; a < data.events[i].attendees.length; a++) {
if (friendResults.includes(fName.toUpperCase())) {
console.log(friendResults);
console.log(data.events[i].eName);
if (data.events[i].attendees.length != 1) {
for (var a = 0; a < data.events[i].attendees.length; a++) {
attendeeText = data.events[i].attendees.join(", ");
}
} else {
attendeeText = data.events[i].attendees;
}
if (data.events[i].attendees.length > 1) {
attendeeText = attendeeText + " are going.";
} else if (data.events[i].attendees.length == 1) {
attendeeText = attendeeText + " is going.";
} else {
attendeeText = "";
}
// Print results
}
}
}
}
}
}
/* document.getElementById("searchFeedback").innerHTML =
"<h4 class='eName'>" + data.events[i].eName + " • " + data.events[i].date + "</h4> <p class='eDesc'>" + data.events[i].description + "<br><sub>" + attendeeText + "<a class='event' onclick='openEventLink(" + i + ");' href=''><b> Check it out here.</b> </a></sub></p><br>"; */
</script>
謝謝你這麼多!
編輯:這是我的數據庫的結構。這裏有更多的東西載入,但是這裏有相關的位。
var data = {
"events": [
{
"eName": "The Dalek Invasion of Earth",
"date": "6.5.2017",
"time": "10-15",
"description": "People assume that time is a strict progression of cause-and-effect... but actually, from a non-linear, non-subjective viewpoint, it's more like a big ball of wibbly-wobbly... timey-wimey... stuff. It is! It's the city of New New York! Strictly speaking, it's the fifteenth New York since the original, so that makes it New-New-New-New-New-New-New-New-New-New-New-New-New-New-New New York. Goodbye...my Sarah Jane! Yeah? Well I'm the Lord of Time. Black tie...Whenever I wear this, something bad always happens.",
"attendees": ["Jack Harkness", "Rose Tyler"],
"eLink": "http://tardis.wikia.com/wiki/The_Dalek_Invasion_of_Earth_(TV_story)"
},
{
"eName": "The Day of the Doctor",
"date": "7.5.2017",
"time": "15-18",
"description": "I'm sorry. I'm so sorry. There's something else I've always wanted to say: Allons-y, Alonso! I don't want to go. Allons-y! What? What?! WHAT?! It is! It's the city of New New York! Strictly speaking, it's the fifteenth New York since the original, so that makes it New-New-New-New-New-New-New-New-New-New-New-New-New-New-New New York. Oh, yes. Harmless is just the word: that's why I like it! Doesn't kill, doesn't wound, doesn't maim. But I'll tell you what it does do: it is very good at opening doors!",
"attendees": ["Amelia Pond", "Donna Noble"],
"eLink": "http://tardis.wikia.com/wiki/The_Day_of_the_Doctor_(TV_story)"
}
]
};
編輯2:我會添加我的表單的HTML,以防萬一它有幫助。
<div class="eventSearch">
<h2>Search for events</h2>
<form id="login" onsubmit="return eventSearch()">
<b>By event name:</b> <input type="text" name="ename" id="ename" size="56" placeholder="i.e. The Time War"><br>
<b>By a friend's name:</b> <input type="text" name="fname" id="fname" size="50" placeholder="i.e. Rose Tyler"><br><br>
<input type="radio" name="dates" value="allDates" checked> All events<br>
<input type="radio" name="dates" value="pastDates"> Past events<br>
<input type="radio" name="dates" value="futureDates"> Future events<br><br>
<button type="submit" id="confSearch" name="confSearch" onclick="event.preventDefault(); eventSearch();"><b>Search</b></button><br><br>
</form>
<div id="searchFeedback"></div>
即使另一個編輯:爲進一步澄清,這裏是我的網頁真人版:http://users.metropolia.fi/~natalisu/Event%20calendar/main.html
由於$('#searchFeedback'),它的打印效果可能只是最後一項。empty();'? – Leguest
刪除該行沒有區別,所以不能這樣。 – Koenshi
好的,那麼'data.events [i]'是什麼?你能在這裏展示嗎? – Leguest