2017-01-13 4 views
0

JavaScriptコードも含まれているhtmlコードで作業していますが、プログラムは動作しますが、必要な結果がすぐに得られ、1つの瞬間に消去されます問題はjavascriptコードに根ざしていません)。私が必要とするのは、それらの結果を画面上で一時停止し、消去しないことです。コードは次のとおりです。javascriptコードを含むhtmlで速すぎる結果

<html> 
<head> 
<title>Geometric operations</title> 
<script> 
     function Calculate(){ 
      var radius=document.forms["form1"]["radius"].value; 
      if (radius==null || radius=="" || isNaN(radius)){ 
       alert("Please give the radius of the circle"); 
    return false; 
    } 
    var radius = parseInt(document.getElementById("radius").value,5); 
    var perimeter = (2 * radius * Math.PI); 
    var embadon = (radius * radius * Math.PI); 

    document.getElementById("perimeter").value = perimeter; 
    document.getElementById("embadon").value = embadon; 
} 
</script> 
</head> 
<body> 
<h1>Calculation of circle perimeter and circle area</h1> 
To calculate the circle perimeter and circle area you need to give the radius and then press the button<b>"Calculation"</b><br> 
<form name="form1" action="" method="" onSubmit="return Calculate()"> 
<pre> 
Circle radius : <input type="text" id="radius" size=5> m<br> 
Circle perimeter : <input type="text" id='perimeter' size=5> m <br> 
Circle area : <input type="text" id='embadon' size=5> m^2<br> 
<input type="submit" name="submit" value="Calculation" onclick="Calculate()"> 
</pre></form> 
</body> 
</html> 

これがなぜ起こるかについての事前のおかげで、ありがとうございます。

+0

送信ボタンはもちろん、ページを再読み込みした、押したときにあなたがあなたのフォームを提出しています。この場合もフォームの提出を防ぐ必要があります。または、最初に適切なタイプのボタンを選択してください。 – CBroe

+0

@CBroe - それは私の初期の考えでしたが、 'onSubmit =" return Calculate() "と' return false; '...ああ、それはifステートメントにあります。押し込みが混乱しています。 – Quentin

+0

素早く答えてくれてありがとう@CBroe私はそれを試してみましょう。 – Newer

答えて

0

実行して、関数のフォームと実行を送信します。関数が送信され、ページがリフレッシュされます。 type="submit"のタイプをtype="button"に変更し、機能にreturn false;を追加します。

<html> 
<head> 
<title>Geometric operations</title> 
<script> 
     function Calculate(){ 
      var radius=document.forms["form1"]["radius"].value; 
      if (radius==null || radius=="" || isNaN(radius)){ 
       alert("Please give the radius of the circle"); 
    return false; 
    } 
    var radius = parseInt(document.getElementById("radius").value,5); 
    var perimeter = (2 * radius * Math.PI); 
    var embadon = (radius * radius * Math.PI); 

    document.getElementById("perimeter").value = perimeter; 
    document.getElementById("embadon").value = embadon; 
    return false; 
} 
</script> 
</head> 
<body> 
<h1>Calculation of circle perimeter and circle area</h1> 
To calculate the circle perimeter and circle area you need to give the radius and then press the button<b>"Calculation"</b><br> 
<form name="form1" action="" method="" onSubmit="return Calculate()"> 
<pre> 
Circle radius : <input type="text" id="radius" size=5> m<br> 
Circle perimeter : <input type="text" id='perimeter' size=5> m <br> 
Circle area : <input type="text" id='embadon' size=5> m^2<br> 
<input type="button" name="submit" value="Calculation" onclick="Calculate()"> 
</pre></form> 
</body> 
</html> 
+0

ところで、誰かが正しいコードを望むなら、書かれたものが正しい結果をここに提供しないので、それはです。 – Newer

+0

「役に立たない」より前に書いたように、私の悪い英語を許してください。 \t ありがとうございました! Rafal Mazgai、答えを送った人の誤解をおかけして申し訳ありませんでしたが、それは有用ではありませんでした! – Newer

0

ここにある:

<html> 
<head> 
<title>Geometric operations</title> 
<script> 
    function Calculate(){ 
     var radius=document.forms["form1"]["radius"].value; 
     if (radius==null || radius=="" || isNaN(radius)){ 
      alert("Please give the radius of the circle"); 
return false; 
} 
var num=2; 
var PI=3.14159; 
var radius=document.forms["form1"]["radius"].value; 
var perimeter = ((num) * (radius) * (PI)); 
var embadon = ((radius) * (radius) * (PI)); 

document.getElementById("perimeter").value = perimeter; 
document.getElementById("embadon").value = embadon; 
return false; 
} 
</script> 
</head> 
<body> 
<h1>Calculation of circle perimeter and circle area</h1> 
To calculate the circle perimeter and circle area you need to give the 
radius and then press the button<b>"Calculation"</b><br> 
<form name="form1" action="" method="" onSubmit="return Calculate()"> 
<pre> 
Circle radius : <input type="text" id="radius" size=5> m<br> 
Circle perimeter : <input type="text" id='perimeter' size=5> m <br> 
Circle area : <input type="text" id='embadon' size=5> m^2<br> 
<input type="button" name="SUBMIT" value="Calculation"onclick="Calculate()"> 
</pre></form> 
</body> 
</html> 
関連する問題