无法搜索数组项
unable to search for array items
在这个程序中,我无法搜索数组中的项。数据库变量已定义。我无法进入搜索功能并运行它。只是好奇为什么会发生这种情况。我可以运行第一个验证函数,但随后会停止,并且不会执行其余的代码。
// Create privatized scope using a self-executing function
(function() {
console.log("hello");
// Variable initialization (DO NOT FIX ANY OF THE BELOW VAR's)
var resultsDIV = document.getElementById("results"),
searchInput = document.forms[0].search,
currentSearch = '';
// Validates search query
var validate = function (query) {
console.log("validate");
// Trim whitespace from start and end of search query
query = query.trim();
// Check search length, must have 3 characters
if (query.length < 3) {
alert("Your search query is too small, try again.");
}else{
search(query);
// (DO NOT FIX THE LINE DIRECTLY BELOW)
searchInput.focus();
}
console.log("test");
};
console.log("outside search function");
// Finds search matches
var search = function (query) {
console.log("In search function");
// split the user's search query string into an array
var queryArray = query.split(" ");
// array to store matched results from database.js
var results = [];
// loop through each index of db array
for (var i = 0, j = db.length; i < j; i++) {
console.log(i);
// each db[i] is a single video item, each title ends with a pipe "|"
// save a lowercase variable of the video title
var dbTitleEnd = db[i].indexOf('|');
var dbItems = db[i].toLowerCase().substring(0, dbTitleEnd);
}
// loop through the user's search query words
// save a lowercase variable of the search keyword
for (var ii = 0, jj = queryArray.length; ii < jj; ii++) {
var qItem = queryArray[ii].toLowerCase();
}
// is the keyword anywhere in the video title?
// If a match is found, push full db[i] into results array
var compare = dbItems.indexOf(qItem);
if (compare !== -1) {
results = results.push(db[i]);
}
results.sort();
// Check that matches were found, and run output functions
if (results.length === 0) {
noMatch();
} else {
showMatches(results);
}
};
// Put "No Results" message into page (DO NOT FIX THE HTML VAR NOR THE innerHTML)
var noMatch = function() {
var html = '' +
'<p>No Results found.</p>' +
'<p style="font-size:10px;">Try searching for "JavaScript". Just an idea.</p>'
;
resultsDIV.innerHTML = html;
};
// Put matches into page as paragraphs with anchors
var showMatches = function (results) {
// THE NEXT 4 LINES ARE CORRECT.
var html = '<p>Results</p>',
title,
url
;
// loop through all the results search() function
for (var i = 0, j = results.length; i < j; i++) {
// title of video ends with pipe
// pull the title's string using index numbers
var titleEnd = results[i].indexOf('|');
title = results[i].subString(0, titleEnd);
// pull the video url after the title
url = results[i].substring(results[i].indexOf('|') + 1, results[i].length);
// make the video link - THE NEXT LINE IS CORRECT.
html += '<p><a href=' + url + '>' + title + '</a></p>';
resultsDIV.innerHTML = html; //THIS LINE IS CORRECT.
}
};
console.log("start of program");
/***** start of program *******/
// The onsubmit event will be reviewed in upcoming Course Material.
// THE LINE DIRECTLY BELOW IS CORRECT
document.forms[0].onsubmit = function(){
var query = searchInput.value;
validate(query);
// return false is needed for most events - this will be reviewed in upcoming course material
// THE LINE DIRECTLY BELOW IS CORRECT
return false;
};
})();
我检查了你的代码,所有的东西都在那里,但不知何故,所有的连接都错误了。您对for
循环的概念似乎完全错误。我做了几个小改动:
-
查询在读取时被修剪,因为我们需要它,而不仅仅是在验证器中
-
验证器:
- 如果查询太短,请将焦点设置回原位
- 它验证了,但也允许在发生错误时继续操作-已更改
- 已更改为isValid()并签入onsubmit处理程序
-
搜索:
- for循环的错误概念
- 检索dbTitleEnd/dbItems,然后用下一个覆盖它们
- 不需要做CCD_ 2,而只需要"results.push(db[i])"
- subString()已更正为subString()
- console.log()消息保留在
请参阅jsFiddle中的示例。
相关文章:
- 使用javascript搜索具有用户输入的数组
- 在mongoose中使用正则表达式在对象数组中进行查询搜索
- 使用数组向下搜索Javascript对象
- 具有嵌套对象数组的 Javascript 对象的递归搜索函数
- jQuery-在数组中搜索类似的元素
- Javascript:使用用户输入搜索数组
- findAndModify,如何对文档进行操作's数组搜索对象并更改字段值
- 在记录数组搜索后追加变量和数组项
- 按第一个字符进行数组搜索
- 更高效的数组搜索
- Javascript数组搜索并删除字符串
- JavaScript中的二进制数组搜索
- 在JavaScript中有更好的方法吗?使用while进行数组搜索
- Javascript中的数组搜索不工作
- 如何用输入数组搜索对象
- 这是更有效的JavaScript数组搜索或MySQL LIKE查询
- Javascript或PHP数组搜索像SQL % search %
- Js数组搜索功能,适用于ie8
- 从Python到Javascript的2D数组搜索
- Javascript数组搜索