javascript - Form button keeps showing results in new window -
Here's a lot new for HTML and javascript. I get the following form and calculate it correctly, but the result appears in a new page. I want to do this on the same page. Not sure what I did wrong here, besides the function, Is there any way to do this? Looks like a lot of work to do a simple calculation, any help would be great.
& lt; Html & gt; & Lt; Top & gt; & Lt; Title & gt;! Help & lt; / Help & gt; & Lt; Script type = "text / javascript" & gt; Add function (x, y) {var x = document.add1.add2.value; Var y = document.add1.add3.value; Var x = number (x); Var y = number (y); Var z = x + y; Return (jade); } & Lt; / Script & gt; & Lt; / Head & gt; & Lt; Body & gt; & Lt; H3 & gt; Help me in overgrowth, you are my only hope! & Lt; / H3 & gt; & Lt; Form name = "AD1" & gt; Number before adding input: & lt; Input type = "number" name = "AD2" & gt; Number 2: & lt; Input type = "number" name = "AD3" & gt; & Lt; Input type = "button" value = "result" onclick = "document.write ('total:' + ('+'. '');" / & Gt; & Lt; / Body & gt; & Lt; / Html & gt;
Do not use data to display, it overwrites the entire document Doing so is preferable to create a new function that will present results in any other element:
& lt; Input type = "button" value = "result" onclick = "showResult ('total:' + () + '.');" / "
and showResult function may be for example:
function showResult (result) {document.getElementById ('Result'). InnerHTML = Results; }
HTML:
& lt; Div id = "result" & gt; & Lt; / Div & gt;
Demo:
Here are some common improvements that you can make your code:
-
String Manuplies Move to show results completely:
& lt; Input type = "button" value = "result" onclick = "showResult ()" />
call
add
from insideshowResults
onclick = "showResult (this.form.add2.value, this.form.add3.value)"
Comments
Post a Comment