我对 javascript 很陌生,所以我可能看不清楚我的问题。我正在尝试创建一个搜索栏,只搜索 3 个不同的项目。我已经构建了我试图调用的函数,但我无法让它在 switch 语句函数中显示该函数。
function doSearch() { //start doSearch
searchvalue = document.searchbox.searchterm.value
switch (searchvalue){ //start switch statement
case "4426": buildProduct(); //this is the part that I can't get to work properly.
break
case "88123": buildProduct();
break
case "2400X": buildProduct();
default: alert("Sorry, please try again.")
break
} //end switch statement
} //end doSearch
搜索框显示很好,它甚至会提醒default
很好,但是当我输入4426
并单击时,它什么也不做。我能够让它工作的唯一方法是添加 adocument.writeln (buildProduct);
而不是buildProduct()
. 表单的 HTML 是
<FORM NAME="searchbox">
<INPUT TYPE="TEXT" NAME="searchterm">
<INPUT TYPE="BUTTON" VALUE="Search" ONCLICK="Javascript:doSearch()">
</FORM>
编辑:这是完整的代码。
function makeHeader() {
var data = "<B>"
data +="<H1>Computers for Sale:</H1>"
data +="</B>"
return(data)
}
function makeFooter() {
var data = "<B>"
data +="<small>Copyright 2013 ComputersforSale</small>"
data +="</B>"
return(data)
}
function buildProduct(prodName, prodID, prodDescription) { //start buildProduct
var data ="<TABLE style='width:100%' BORDER=1 CELLPADDING=10><tr><td>"
data += makeHeader()
data += "</td></tr>"
data += "<tr><td>"
data += prodName + '</br>' + prodID + '<br>' + prodDescription
data += "</td></tr>"
data += "<tr><td>"
data += makeFooter()
data += "</td></tr>"
data += "</table>"
return(data)
document.writeln(buildProduct)
document.close()
} //end buildProduct
function doSearch() { //start doSearch
searchvalue = document.searchbox.searchterm.value
switch (searchvalue){ //start switch statement
case "4426":
buildProduct();
break;
case "88123":
buildProduct();
break;
case "2400X":
buildProduct();
break;
default:
alert("Sorry, please try again.")
break;
} //end switch statement
} //end doSearch
该函数的 HTML 链接:
<a href= "Javascript:buildProduct('HP 4426', '4426','Newest and Best Laptop from HP Computer')">Item #1 HP 4426</a></br>
<a href= "Javascript:buildProduct('IBM 88123 ThinkPad', '88123','IBM micro Laptop Computer')">Item #2 IBM 88123 ThinkPad</a></br>
<a href= "Javascript:buildProduct('Dell Dimension 2400', '2400X','A fast 2.4 ghz computer, 80 GB HD, 256 Meg, CDRW and DVD.')">Item #3 Dell Dimension 2400</a>