简体   繁体   中英

Why these javascript function won't start?

I don't know why these JavaScript functions won't start. I've got a very tiny knowledge of JavaScript so I'm asking if I made some big mistakes.

Here is my code:

<!DOCTYPE html>
<html>
<head>
<title>prova</title>
<script>
        var ind = 0;
        var mat = new Array(10);

        for(int i=0;i<10;i++)
            mat[i] = new Array(2);

        function addValue()
        {
            mat[ind]["nome"] = document.modulo.nome.value;
            mat[ind]["cognome"] = document.modulo.cognome.value;
            ind++;
            alert("Valore aggiunto con successo!");
        }

        function showValues()
        {
            var pag = window.open("","page","width=250,height=200");
            pag.document.write("<table>");
            for(int i=0;i<10;i++)
            {
                pag.document.write("<tr>");
                pag.document.write("<td>" + mat[i]["nome"] + "</td>");
                pag.document.write("<td>" + mat[i]["cognome"] + "</td>");
                pag.document.write("</tr>");
            }
            pag.document.write("</table>");
        }
 </script>
 </head>
 <body>
    <h1>Matrice associativa</h1>
    <form name="modulo" method="post">
        <b>Nome: </b><input type="text" name="nome"/><br/><br/>
        <b>Cognome: </b><input type="text" name="cognome"/><br/><br/>
        <input type="button" name="aggiungi" value="Aggiungi" onclick="addValue();"/>
        <input type="button" name="visualizza" value="Visualizza" onclick="showValues();"/>         
    </form>
</body>
</html>

You should check the console.

The problem in this case is that you wrote for (int i=0 ... and let should have been used instead of int . The error is present twice in your code.

With that fixed it works as expected.

检查您是否为循环编写了int我这是错误的,因为您必须使用javascript变量声明是javascript变量。

The error is with the for loops, you don't need to use data types here.

Replace the codes.

for (i = 0; i < 10; i++)
    mat[i] = new Array(2);

and

for (i = 0; i < 10; i++)
{
    pag.document.write("<tr>");
    pag.document.write("<td>" + mat[i]["nome"] + "</td>");
    pag.document.write("<td>" + mat[i]["cognome"] + "</td>");
    pag.document.write("</tr>");
}

gif图像

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM