我需要通过简单的javascript来阻止我的页面刷新

时间:2015-01-31 14:34:05

标签: javascript

所以我输入了这一切,唯一可以使这段代码变得完美的是无刷新代码。我需要它在Javascript,没有Jquery,Ajax或PHP。因为我还没有理解这些语言。好吧,这是我的代码。

function welkom()//this function is in the <body onload="">
{
var naam = prompt("Wat is je naam");
document.getElementById("welkom").innerHTML="Welkom " + naam + " en succes    met de tafels.";
}
function test()//this function is in the <body onload="">
{
    var nummer = document.getElementById("cijfers").value;
    document.getElementById("resultaat").innerHTML = "";
    var form = "<table><form>";
    for (var x = 1;x<=10;x++)
    {
        form += "<tr><td>" + x + "x" + nummer + "= </td>";
        form += "<td><input type='text' id='vraag[" + x + "]' /></td></tr>";
    }
    form += "</table><input type='submit' value='Controleer je antwoorden' onclick='controleer()'></form>";
    document.getElementById("resultaat").innerHTML = form;
}
        function controleer()//this is the function that refreshes my page.
        {
            var tafelNr = document.getElementById("cijfers").value;
            var goed = 0;
            var fout = 0;

            for (var teller=1;teller<=10;teller++)
            {
                var tafelNr = document.getElementById("cijfers").value;
                var goedantwoord = tafelNr * teller;
                var antwoord = document.getElementById("vraag[" + teller + "]").value;

                if (goedantwoord == antwoord)
                {
                    document.getElementById("vraag[" + teller + "]").style.backgroundColor="#00ff00";
                    goed++
                }
                else
                {
                    document.getElementById("vraag[" + teller + "]").style.backgroundColor="#ff0000";
                    fout++
                }
            }
            if (goed <=6)
            {
                alert("Je had " + goed + " vragen goed en " + fout + " vragen fout. \nVraag aan de meester of juf om een oefenblaadje.");
            }
            else
            {
                alert("Je had " + goed + " vragen goed en " + fout + " vragen fout. \nGoed gedaan!")
            }
        }

1 个答案:

答案 0 :(得分:1)

onclick='controleer()'更改为onclick='controleer(); return false;'

或者,如果您不打算提交数据,请不要使用<form>