javascript 如何从javascript中的switch语句调用函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15401186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to call a function from a switch statement in javascript
提问by Leesh
I'm pretty new at javascript so I might not see my problem clearly. I'm trying to create a search bar that simply searches for say, 3 different items. I've already built my function that I am trying to call, but I can't get it to display the function in the switch statements function.
我对 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
The search box displays fine, and it even alerts the default
just fine, but when I enter 4426
and click, it does nothing. The only way I have been able to get it to work somewhat is if I add a document.writeln (buildProduct);
instead of buildProduct()
.
And the HTML for the form is
搜索框显示正常,它甚至警告default
就好了,但是当我输入4426
并单击时,它什么也不做。我能够让它在某种程度上工作的唯一方法是如果我添加一个document.writeln (buildProduct);
而不是buildProduct()
. 表单的 HTML 是
<FORM NAME="searchbox">
<INPUT TYPE="TEXT" NAME="searchterm">
<INPUT TYPE="BUTTON" VALUE="Search" ONCLICK="Javascript:doSearch()">
</FORM>
EDIT: This is the full code.
编辑:这是完整的代码。
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
The HTML links for the function:
该函数的 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>
回答by Surendra Jnawali
I think break need semicolon(;)in switch statement
我认为在 switch 语句中break 需要分号(;)
回答by Devang Rathod
You forgotten to break
in case "2400X"
and also break need ;
你忘记break
了case "2400X"
,也打破了需要;
function doSearch(){
searchvalue = document.searchbox.searchterm.value
switch (searchvalue){
case "4426":
buildProduct(); //this is the part that I can't get to work properly.
break;
case "88123":
buildProduct();
break;
case "2400X":
buildProduct();
break;
default:
alert("Sorry, please try again.")
break;
}
}
回答by Grant Eagon
I think I understand the problem. Consider this block of code:
我想我明白这个问题了。考虑这个代码块:
switch (expression) {
case expression:
break;
default:
}
Your case expression needs to match the value that the switch expression expects.
您的 case 表达式需要匹配 switch 表达式所期望的值。
So, if run the following code:
因此,如果运行以下代码:
var minerals = ['dirt', 'rock', 'gold'];
for (var i = 0; i < minerals.length; i++) {
var mineral = minerals[i];
switch (mineral) { // this will see a string value, in this example, for each item of the array
case isGold (mineral):
console.log('We struck gold!')
break;
default:
console.log('No gold found. :(')
}
}
function isGold (mineral) {
if (mineral === 'gold') {
return 'gold'; // this needs to match what the switch expression sees, which is a string in this case, not a Boolean.
}
}
This will be the output:
这将是输出:
// -> No gold found. :(
// -> No gold found. :(
// -> We struck gold!