使用Javascript提示动态创建表(不工作)

时间:2015-06-06 23:32:50

标签: javascript html html-table

这是我到目前为止,但它甚至没有提示用户,它显示一条细黑线(我认为是边界)

<html>
    <head>
        <meta charset = "utf-8">
        <title>Dynamically creating a table</title>
    </head>     
    <body>
        <table style = "width:100%" cellspacing="5" border="2">
        <script type = "text/javascript">
            var running = true; //boolean to determine if we should continue running
            while(running) {
                var i = 0;
                var input; //variable that holds the input values   
                input = window.prompt("Please enter your product number then quantity sold, seperated by a comma. Hit Enter when you are done.");
                var split;
                var num1;
                var num2;
                if(input != "") {
                    split = input.split(',');
                    num1 = split[0];
                    num2 = 0;
                    if(split.length > 1) {
                        num2 = split[1].trim();
                    }
                }

                switch(num1) {
                    case 1:
                        // unimportant calculations
                        break;
                    case 2:
                        // unimportant calculations
                        break;
                    default:
                        document.writeln("<tr>");
                        document.writeln("<td>text</td>");
                        document.writeln("<td>text</td>");
                        document.writeln("</tr>");
                        document.writeln("<tr>");
                        document.writeln("<td>text1</td>");
                        document.writeln("<td>text2</td>");
                        document.writeln("<td>text3</td>");
                        document.writeln("</tr>");
                        running = false;
                }
            }
        </script>
        </table>
    </body>
</html>

我尝试将它包含在头部和身体中,我也尝试在脚本和脚本之外声明表标记。

2 个答案:

答案 0 :(得分:1)

您的代码中存在一些错误,例如:

<table style = "width:100%" cellspacing="5" border="2">

你不能将html代码放在head中,你需要在body上写这个。

var input[i]; //variable that holds the input values  

您无法定义包含密钥的变量。正确的方法是:

var input = [];

其他

switch(num1):
  // ... code

正确的方法是:

switch(num1) {
  //  ... code
}

您可以使用html validator检查html错误,如果您使用的是Firefox或Chrome等现代浏览器,则可以按f12访问javascript错误。

答案 1 :(得分:1)

您的代码有几个javascript错误。试试这个开头:

<html>
    <head>
        <meta charset = "utf-8">
        <title>Dynamically creating a table</title>
        <table style = "width:100%" cellspacing="5" border="2">
        <script type = "text/javascript">
            var running = true; //boolean to determine if we should continue running
            while (running) {
                var input = window.prompt("Please enter your two numbers seperated by a comma.");
                var split = input.split(',');
                var num1= split[0];
                var num2= input[1];

                switch(num1) {
                    case '1':
                        document.writeln("<tr>");
                        document.writeln("<td>text</td>");
                        document.writeln("<td>text</td>");
                        document.writeln("</tr>");
                    case '2':
                        document.writeln("<tr>");
                        document.writeln("<td>text1</td>");
                        document.writeln("<td>text2</td>");
                        document.writeln("<td>text3</td>");
                        document.writeln("</tr>");
                    case '99':
                        running = false;
                    }
            }
        </script>
        </table>
    </head>     
    <body>
    </body>
</html>