JavaScript搜索数据库
我正在做一个搜索查询,搜索一个非常小的数据库。我终于让代码运行,没有错误,但我无法看到任何结果。我只是好奇我做错了什么。任何帮助都将不胜感激JavaScript搜索数据库,javascript,Javascript,我正在做一个搜索查询,搜索一个非常小的数据库。我终于让代码运行,没有错误,但我无法看到任何结果。我只是好奇我做错了什么。任何帮助都将不胜感激 // Create privatized scope using a self-executing function (function() { // Variable initialization (DO NOT FIX ANY OF THE BELOW VAR's) var resultsDIV = document.getElementById("
// Create privatized scope using a self-executing function
(function() {
// 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) {
// Trim whitespace from start and end of search query
while(query.charAt(0) ===" ") {
query = query.substring(1, query.length);
};
while(query.charAt(query.length-1) === " ") {
query = query.substring(0, query.length-1);
};
// Check search length, must have 3 characters
if(query.length < 3){
alert("Your search query is too small, try again.");
// (DO NOT FIX THE LINE DIRECTLY BELOW)
searchInput.focus();
return;
};
search(query);
};
// Finds search matches
var search = function(query) {
// split the user's search query string into an array
var queryArray = query.join(" ");
// 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++){
// 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 dbitem = 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 = dbitem.indexOf(qitem);
if(compare !== -1){
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
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.
};
// 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;
};
})();
//使用自执行函数创建私有化作用域
(功能(){
//变量初始化(不要修复以下任何变量)
var resultsDIV=document.getElementById(“结果”),
searchInput=文档。表单[0]。搜索,
currentSearch=“”
;
//验证搜索查询
var validate=函数(查询){
//从搜索查询的开始和结束修剪空白
while(query.charAt(0)==“”){
query=query.substring(1,query.length);
};
while(query.charAt(query.length-1)==“”){
query=query.substring(0,query.length-1);
};
//检查搜索长度,必须有3个字符
if(query.length<3){
警报(“您的搜索查询太小,请重试。”);
//(不要直接在下方固定管线)
searchInput.focus();
返回;
};
搜索(查询);
};
//查找搜索匹配项
变量搜索=函数(查询){
//将用户的搜索查询字符串拆分为数组
var queryArray=query.join(“”);
//用于存储database.js中匹配结果的数组
var结果=[];
//循环遍历db数组的每个索引
对于(var i=0,j=db.length;i虽然我可能没有您那么先进,但使用PHP进行MySQL搜索并通过Ajax返回结果可能会更简单。互联网上有很多示例和教程,所以它可能会有所帮助
成功!有此注释://将…拆分为一个数组
,然后执行:var queryArray=query.join(“”;
??看起来您需要拆分(“”)
这里。//下面这行是正确的
肯定是我遇到过的最无用的注释之一。你可以像这样修剪空白:query=query.trim();