2015-11-22 51 views
-2

還好這裏的問題 - ..我嘗試過,但我的js不能正常使用IDK的我哪裏錯了here is the questionHTML ---的JavaScript/JSON

問題是之後的JS執行時沒有按」運行...像IDK那樣存在問題;我知道它負載,但IT一點兒也不工作

<html> 
<head> 
<script src="q2.js" type="text/javascript"> </script> 
</head> 
<div > Input 1 <input type="text" id ="input1"></div> 
    <div> Input 2 <input type="text" id ="input2"> </div> 
    <div> Result <div id="result"> </div></div> 
    <button onclick= "compute()">Compute</button> 

</body> 
</html> 

JS的是在這裏

function compute(){ 
var n = (document.getElementById("input1").value; 
var m = document.getElementById("input2").value; 

var i,j; 

if (Number(n)){ 
} 
else { 
    alert("Error! Please put a valid Number - on input 1 "); 
    } 
    if (Number(m)){ 
    } 
    else { 
    alert("Error! Please put a valid Number - on input 2 "); 
    } 

     for(i = 0; i < n; i++){ 
     for(j = 0; j < m; j++){ 
      result.innerHTML += "X"; 
      if(j == (m-1)){ 
      result.innerHTML += "<br />"; 
      } 
     } 
    } 
    } 
+2

'這裏是question'外部圖像的鏈接對於這個問題的未來讀者是沒用的,如果這個圖像消失 –

回答

2

result.innerHTML += "X";

你忘了設置變量resultvar result = document.getElementById("result");

而且有一個loneley (var n = (document.getElementById("input1").value;這將通過語法錯誤

,你可能要清除「結果」 -container再次調用函數時的內容:result.innerHMLT = ''

function compute() { 
 
    var n = document.getElementById("input1").value; 
 
    var m = document.getElementById("input2").value; 
 
    var result = document.getElementById("result"); 
 
    
 
    result.innerHMLT = '' 
 

 
    var i, j; 
 

 
    if (Number(n)) {} else { 
 
    alert("Error! Please put a valid Number - on input 1 "); 
 
    } 
 
    if (Number(m)) {} else { 
 
    alert("Error! Please put a valid Number - on input 2 "); 
 
    } 
 

 
    for (i = 0; i < n; i++) { 
 
    for (j = 0; j < m; j++) { 
 
     result.innerHTML += "X"; 
 
     if (j == (m - 1)) { 
 
     result.innerHTML += "<br />"; 
 
     } 
 
    } 
 
    } 
 
}
<div>Input 1 
 
    <input type="text" id="input1"> 
 
</div> 
 
<div>Input 2 
 
    <input type="text" id="input2"> 
 
</div> 
 
<div>Result 
 
    <div id="result"></div> 
 
</div> 
 
<button onclick="compute()">Compute</button>

+0

謝謝@Codeisir ....只是意識到我錯過了那和Tnx的幫助 – kiko