admin管理员组

文章数量:1263220

Got a problem. this is my code:

<html>

<body>
<form>
<p>13-7: <input id="in1" type="text"" /><input type="submit" onclick="check(6, 'in1', 'out1')" value="Tjek!"/></p>
<p id="out1"></p>
<p>20-7: <input id="in2" type="text"" /><input type="submit" onclick="check(13, 'in2', 'out2')" value="Tjek!" /></p>
<p id="out2"></p>



<script type="text/javascript">

function check(facit, input, output) {
    var answer, evaluation;
    answer = document.getElementById(input).value;
    evaluation = (answer == facit) ? "Correct" : "Wrong";
    document.getElementById(output).innerHTML = evaluation;
    return false;
}

</script>
</form>
</body>

</html>

When I click the submit-button, the 'correct/wrong' shows only for a moment. I want it to stay on the site, any advice?

Got a problem. this is my code:

<html>

<body>
<form>
<p>13-7: <input id="in1" type="text"" /><input type="submit" onclick="check(6, 'in1', 'out1')" value="Tjek!"/></p>
<p id="out1"></p>
<p>20-7: <input id="in2" type="text"" /><input type="submit" onclick="check(13, 'in2', 'out2')" value="Tjek!" /></p>
<p id="out2"></p>



<script type="text/javascript">

function check(facit, input, output) {
    var answer, evaluation;
    answer = document.getElementById(input).value;
    evaluation = (answer == facit) ? "Correct" : "Wrong";
    document.getElementById(output).innerHTML = evaluation;
    return false;
}

</script>
</form>
</body>

</html>

When I click the submit-button, the 'correct/wrong' shows only for a moment. I want it to stay on the site, any advice?

Share Improve this question edited Jul 1, 2012 at 20:43 Esailija 140k23 gold badges279 silver badges328 bronze badges asked Jul 1, 2012 at 20:33 user114158user114158 1971 gold badge1 silver badge6 bronze badges 1
  • 1 Just get rid of the opening form tag, the closing tag is missing and it seems you aren't using the form for anythying useful anyway. The controls don't have names so can't be successful. – RobG Commented Jul 2, 2012 at 0:40
Add a comment  | 

5 Answers 5

Reset to default 17

The submit buttons are submitting your form (so reloading your page).

Change the buttons to type="button".

Your buttons are submit buttons (<input type="submit">) so they will submit the form and refresh the page on each click. Change your buttons to <input type="button"> instead.

Change the onclick function from:

onclick="check(13, 'in2', 'out2')"

to:

onclick="return check(13, 'in2', 'out2')"

Alternatively, if you don't want the form to be submitted, as you have two Submit buttons, it is better to use:

<input type="button" />

Just change onclick from this

onclick="check(6, 'in1', 'out1')"

to

onclick="check(6, 'in1', 'out1'); return false;"

Do the same way for the other one too.

Remove return false; on the check function.

Refer LIVE DEMO

The form is being submitted and returned with the original page.

What do you want to do?

I dont want it returned with the original page .. I want it to say 'correct' og 'wrong' below the input 

If you don't want to submit the data then do as minitech suggests, change the type from submit to button.

If you want to save the data you will need to write server side code and then create html with those values filled in (on the server side.)

本文标签: javascripttext disappears rapidly after click on 39submitbutton39Stack Overflow