0
我得到了一個任務,將用戶在表單文本框中輸入的2個矩陣相乘,如下所示:
[[1,2,3],[4,5,6],[7,8,9 ]]
所以我創建了一個var matrix1和matrix2,它們通過一個命令document.getElementById(「matrixID」).value獲取字符串值,但我不知道如何將它們分隔開來,創建一個數組或者一個變量的矩陣(設置1,2,3 ...作爲單獨的矩陣/數組值)。
我不允許使用JQuery(儘管我不認爲它是需要的)。當前的代碼看起來像這樣:
從表單輸入中乘以2個矩陣。 (Javascript)
<!DOCTYPE html>
<html>
<head>
<title>Matrix multiply</title>
<script type="text/javascript">
function calc(){
var matrix1string = document.getElementById("m1").value;
var matrix2string = document.getElementById("m2").value;
}
</script>
</head>
<body>
<form>
Matrix 1: <input type="text" id="m1" name="mat1" style="width: 500px"><br>
Matrix 2: <input type="text" id="m2" name="mat2" style="width: 500px"><br>
<input type="button" value="Calculate" onclick="calc();"><br><br>
-Result: <input type="text" id="rez" name="mat2" style="width: 500px" readonly>
</form>
</body>
</html>
啊,謝謝!我如何使用這些矩陣的元素?與矩陣1 [0],矩陣1 [1]等,或者是從這一點(矩陣[0] [0]等)的真矩陣。 – NemanjaT 2014-10-11 11:26:58
從這一點上它是一個二維數組,因此您可以訪問元素像這樣:matrix1 [0] [1]或者matrix2 [2] [1] – friedi 2014-10-11 11:29:27
啊,非常感謝! :)編輯:它將被接受~6分鐘。 – NemanjaT 2014-10-11 11:29:51