我如何结合这两个相似的功能?

时间:2015-01-29 20:06:21

标签: javascript forms if-statement

我创建了两个运行类似IF语句的函数,一个显示游戏的成本,另一个显示其名称。我使用“onclick”在我的表单中显示两个值。这一切都有效,但遭受臃肿。有没有办法将两个函数结合起来缩短脚本?

        function GameTitle() {
var textboxValue = document.getElementById("game_id").value;
    var message;
    if (textboxValue == 1) {
        message = "Fantasy World";
    } else if (textboxValue == 2) {
        message = "Sir Wags A Lot";
    } else if (textboxValue == 3) {
        message = "Take a Path";
    } else if (textboxValue == 4) {
        message = "River Clean Up";
    } else if (textboxValue == 5) {
        message = "PinBall";
    } else if (textboxValue == 6) {
        message = "Ghost girl";
    } else if (textboxValue == 7) {
        message = "Dress Up";
    } else if (textboxValue == 8) {
        message = "Where is my hat?";
    } else {
        message = "Invalid ID";
    }
document.getElementById("game_title").value = message;
}

//Display price of Game
    function Cost() {
var textboxValue = document.getElementById("game_id").value;
    var cost;
    if (textboxValue == 1) {
        cost = "0.99";
    } else if (textboxValue == 2) {
        cost = "0.99";
    } else if (textboxValue == 3) {
        cost = "1.99";
    } else if (textboxValue == 4) {
        cost = "1.99";
    } else if (textboxValue == 5) {
        cost = "3.99";
    } else if (textboxValue == 6) {
        cost = "3.99";
    } else if (textboxValue == 7) {
        cost = "1.99";
    } else if (textboxValue == 8) {
        cost = "1.99";
    } else {
        cost = "Invalid ID";
    }
document.getElementById("cost").value = cost;
}

3 个答案:

答案 0 :(得分:3)

您应该为此

使用一组对象
var games = [
 {
  message : "Fantasy World",
  cost : 0.99
 },
 {
  message : "Sir Wags A Lot",
  cost : 0.99
 },
 {
  message : "Take a Path",
  cost : 1.99
 },
 {
  message : "River Clean Up",
  cost : 1.99
 },
 {
  message : "PinBall",
  cost : 3.99
 },
 {
  message : "Ghost girl",
  cost : 3.99
 },
 {
  message : "Dress Up",
  cost : 1.99
 },
 {
  message : "Where is my hat?",
  cost : 1.99
 }
];

然后在你的函数中你可以接受并且id,在数组中引用它的索引(基于0来偏移1)并使用该对象。

function Display(){
 var textboxValue = parseInt(document.getElementById("game_id").value,10);
 document.getElementById("game_title").value = 
  textboxValue-1 < games.length ? games[textboxValue-1].message : "Invalid ID";
 document.getElementById("cost").value = 
  textboxValue-1 < games.length ? games[textboxValue-1].cost : "Invalid ID";
}

答案 1 :(得分:2)

您基本上想要立即对这两个字段进行更新。由于您的驱动变量对于两个因素(标题和成本)都是一个(game_id),因此您应该一次更新这两个字段。所以,如下所示:

&#13;
&#13;
 function updateGameTitleAndCost() {
   var textboxValue = document.getElementById("game_id").value;
   var message, cost;
   if (textboxValue == 1) {
     message = "Fantasy World";
     cost = "0.99";
   } else if (textboxValue == 2) {
     message = "Sir Wags A Lot";
     cost = "0.99";
   } else if (textboxValue == 3) {
     message = "Take a Path";
     cost = "1.99";
   } else if (textboxValue == 4) {
     message = "River Clean Up";
     cost = "1.99";
   } else if (textboxValue == 5) {
     message = "PinBall";
     cost = "3.99";
   } else if (textboxValue == 6) {
     message = "Ghost girl";
     cost = "3.99";
   } else if (textboxValue == 7) {
     message = "Dress Up";
     cost = "1.99";
   } else if (textboxValue == 8) {
     message = "Where is my hat?";
     cost = "1.99";
   } else {
     message = "Invalid ID";
     cost = "Invalid ID";
   }

   document.getElementById("game_title").value = message;
   document.getElementById("cost").value = cost;
 }
&#13;
&#13;
&#13;

答案 2 :(得分:1)

您可以使用简单的地图替换IF:

values = {
  "1": "0.99",
  "2": "0.99",
  ...
}

然后用:

调用它
cost = values[textboxValues]

所以你只需要2个地图就可以了。如果在上面的行中找不到成本,则表示它应该是“无效ID”值。

更新

values = {
  "1": {
    cost: "0.99",
    name: "Sir Bunny Hopings"
  },
  "2": {..},
  ...
  }

Game = function() {
  var i = document.getElementById("game_id").value,
      game;

  game = values[id] || {cost: "Invalid ID", name: "Invalid iD"};

  document.getElementById("cost").value = game.cost;
  document.getElementById("game_title").value = game.name;
}