如何从javascript中的switch语句调用函数

时间:2013-03-14 04:24:41

标签: javascript switch-statement

我是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并点击时,它什么都不做。我能够让它工作的唯一方法是添加document.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>

3 个答案:

答案 0 :(得分:4)

我认为 break需要在switch语句中使用分号(;)

答案 1 :(得分:1)

您忘记了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; 
        }
    }

答案 2 :(得分:1)

我想我明白了这个问题。考虑一下这段代码:

    switch (expression) {
        case expression:

            break;
        default:

    }

您的case表达式需要与switch表达式所期望的值匹配。

因此,如果运行以下代码:

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.
    }
}

这将是输出:

// -> No gold found. :(
// -> No gold found. :(
// -> We struck gold!