2012-02-26 61 views
0

我已經作出了形式與posibility來選擇不同的值(x和y):什麼是從表格中讀取的最佳方式?

<form> 
    X: <select id="value_x"> 
    </select> 
    Y: <select id="value_y"> 
    </select> 
</form> 
<div id="result_z"></div> 

在我有多個選項中選擇,例如爲5,20,26等。

現在我做了一個Javascript代碼,它從兩個選擇中讀取值,並將其存儲到兩個變量中。在一個空div(result_z)中,值Z應該被顯示。在Excel文檔我已Z的結果例如:

***12*** - ***14*** - ***16*** - ***25*** - ***30*** - ***40*** 
***14*** - 36 - 12 - 12 - 23 - 28 
***16*** - 45 - 34 - 34 - 34 - 35 
***18*** - 47 - 56 - 46 - 56 - 48 
***20*** - 89 - 78 - 56 - 78 - 70 

你可以看到,如果X = 14且Y = 16的Z值應爲45,並且如果X = 30且Y = 20的Z值應該是78.但是在JavaScript中創建該表格的最佳方式是什麼? MySQL數據庫將是最好的選擇,但我希望沒有數據庫的解決方案。那可能嗎?

非常感謝!

回答

1

您可以使用一個二維數組來存儲您的表格。 然後給出12,14,16 ...指標。你也可以使用自制對象或簡單的數組。 然後,當您選擇值時,您可以查找結果並將其顯示在div中。

這樣的:

 <html> 
<head> 
    <script type="text/javascript"> 
     var p = { 
      table: [[16, 32], [8, 19]], 
      horizontal: [12, 15], 
      vertical: [6, 14], 
      selectOnchange: function() { 
       var x = document.getElementById("x").value; 
       var y = document.getElementById("y").value; 
       document.getElementById("z").value = this.table[Number(x)][Number(y)]; 
      } 
     }; 
    </script> 
</head> 
<body> 
    <div> 
     x: <select id="x" onchange="p.selectOnchange()"><option value="0">12</option><option value="1">15</option></select><br /> 
     y: <select id="y" onchange="p.selectOnchange()"><option value="0">6</option><option value="1">14</option></select><br /> 
     <input id="z"/> 
    </div> 
</body> 
</html> 
1

怎麼樣二維數組,其中x和y是你的鑰匙:

var myarray = new Array(); 
myarray[14] = new Array(); 
myarray[14][14] = 36; 
myarray[14][16] = 45; 
... 

alert(myarray[14][16]); // Get Z-value for X: 14, Y: 16 
相關問題