1
我的目標是提示用戶輸入使用Javascript生成表格的行數和列數(請參閱下面的代碼)。但是,行數/列數的範圍應該從1到10(使用parseInt()
& isNaN
)。如果用戶輸入超出範圍的數字,則應該有一個提示「行號超出範圍 - 再試一次,頁面將被重新加載」,然後使用「location.reload()
」。我試了很多次,但都失敗了。我是一名Javascript初學者,請幫助我。如何檢查用戶輸入的範圍?
<script type="text/javascript">
var rows;
var cols;
do
{
rows = prompt("How many rows? 1-10");
}
while (isNaN(rows));
do
{
cols = prompt("How many columns? 1-10");
}
while (isNaN(cols));
</script>
<style type="text/css">
<!--
table {
border-spacing: 0px;
width: 500px;
border-collapse: collapse;
margin: 20px auto;
}
td {
border: solid 1px grey;
}
-->
</style>
</head>
<body>
<table>
document.write('<table border="1" cellspacing="1" cellpadding="2">');
for(var i = 1; i <= rows; i++)
{
document.write('<tr>');
for(var x = 1; x <= cols; x++)
{
document.write("<td>"+ (i * x) + "</td>");
}
document.write('</tr>');
}
document.write('</table>');
-->
</script>
問JS問題的最佳方式是我們http://jsfiddle.net/。在網站上上傳代碼並在此處添加鏈接,這樣人們可以直接更改代碼並快速查看該操作的結果。 – alykhalid