2017-10-01 48 views
1

我正在完成一個包含以下代碼的項目。所有需要解決的問題都是每條線的開始。例如,如果我使用輸入「8」,它不會顯示完整的工作線,而是每行都以乘號開始。對於有子彈點的另一方也是如此。我希望保留要點,但也要在每條線上輸入我的輸入值。我嘗試過使用一個id作爲我的價值,但它並不成功。抓取字符串輸入的值

任何意見或援助將不勝感激,

乾杯。

<html> 
<head> 
</head> 
<body> 

<h2 style="color:blue;">Enter an integer from 1 to 9:</h2> 

<input type="text" id="multipleOf" value=""> 

<button onclick="multiplier()">Generate Times Table</button> 

<div style="color:blue;" id="multTable"> <h2>Multiplication Table</h2> </div> 

<script type="text/javascript"> 
//<![CDATA[ 
function multiplier() { 
var mult = document.getElementById('multipleOf').value; 
var str = '<table border="0" width="100%"><tr><td>'; 
str += ''; 
str += ''; 
for (var i=1; i<10; i++) { 
str += '<br />'; 
str += '' + ' x ' + i + ' = '; 
str += mult * i; 
str += ''; 
str += ''; 
} 
str += '</td><td>'; 
for (var i=1; i<10; i++) { 
str += '<br />'; 
str += '&bull;' + ' x ' + i + ' = '; 
str += mult * i; 
str += ''; 
str += ''; 
} 
str += '' 
str += '</td></tr></table>'; 
document.getElementById('multTable').innerHTML = '<h2>Multiplication 
Table</h2>'+str; 
} 
//]]> 

</script> 

</body> 
</html> 

回答

0

mult已經包含第一個操作數,所以它只是需要在循環被添加到str

for (var i = 1; i < 10; i++) { 
    str += "<br />"; 
    str += mult + " x " + i + " = "; // Concatenating mult here. 
    str += mult * i; 
    str += ""; 
    str += ""; 
} 

for (var i = 1; i < 10; i++) { 
    str += "<br />"; 
    str += mult + "&bull;" + " x " + i + " = "; // Concatenating mult here. 
    str += mult * i; 
    str += ""; 
    str += ""; 
}