2012-05-08 20 views
5

htmlでフォームに値を入力する方法を知りました。 ユーザーがサイトに移動すると、すでにテキストボックスに数字が表示されていて、それらの数字を使用して「追加ゲーム」が行われます。 これは私がこれまで持っているものです。JavaScript - スクリプトからhtmlフォームに値を書き込む

 <script type="text/javascript"> 

    function addSubmit() { 
//trying to have num1 and num2 write into the html form 
    var num1= Math.floor(89+Math.random()*11); 
    var num2 = Math.floor(89+Math.random()*11); 
    /*num1 = value1 
    num2 = value2*/ 
    document.getElementById("value1").value = num1; 
    document.getElementById("value2").value = num2; 

    var guess = document.getElementById("submit").value; 
    var answer = num1 + num2; 
    //have to write this to main window 
    if (guess == answer) 
    document.writeln("Congratulations you answered correctly! The answer to: "+num1+"+"+num2+"= "+answer); 
    else 
    document.writeln("Nice try, but the correct answer to: "+num1+"+"+num2+"= "+answer); 
    } 

    function addGiveUp() { 
    var num1 = Math.floor(89+Math.random()*11) 
    var num2 = Math.floor(89+Math.random()*11) 
    document.addition.value1.value=num1; 
    document.addition.value2.value=num2; 
    var guess = document.getElementById("submit").value; 
    var answer = (document.getElementById("value1").value) + (document.getElementById("value2").value); 
    document.writeln("Never give up! The answer to: "+num1+"+"+num2+"= "+answer); 
    } 
    </script> 
    <form name="addition" action="" method="get"> 
    <table border="1"> 
      <tr> 
     <td>Value 1:</td> <td><input type="text" name="value1" id="value1"/></td> 
       </tr> 
      <tr> 
     <td>Value 2:</td> <td><input type="text" name="value2" id="value2"/></td> 
       </tr> 
      <tr> 
     <td>Answer:</td> <td><input type="text" id="answer" /></td> 
       </tr> 
      <tr><td><input type="button" value="Submit" onClick="addSubmit()" id="submit" /></td><td><input type="button" value="Give Up" onClick="addGiveUp()" /></td></tr> 
      </table> 
    </form> 

私は任意の助けに感謝!ありがとう!

+1

何が問題ですか?あなたはどこにいらっしゃいますか?この宿題ですか?あなたはちょうどレビューをお探しですか?ようこそSO – rlemon

+0

それは宿題です。私はコード内にあるHTMLフォームに値を書き込む方法についています。私はフォームにスクリプトと感謝の形に移動する必要がありますを書くには、JavaScriptコードを使用したい! – Gcap

答えて

3

フォームが作成された後も、あなたがこのスクリプトを置くことができますあなたのための乱数を生成し、 コードについてはこちらを参照してくださいフォームでそれらを置く

<form name="addition" action="" method="get"> 
      <table border="1"> 
        <tr> 
       <td>Value 1:</td> <td><input type="text" name="value1" id="value1"/></td> 
         </tr> 
        <tr> 
       <td>Value 2:</td> <td><input type="text" name="value2" id="value2"/></td> 
         </tr> 
        <tr> 
       <td>Answer:</td> <td><input type="text" id="answer" /></td> 
         </tr> 
        <tr><td><input type="button" value="Submit" onClick="addSubmit()" id="submit" /></td><td><input type="button" value="Give Up" onClick="addGiveUp()" /></td></tr> 
        </table> 
</form> 

<!-- This is the script--> 
<script type="text/javascript"> 
     document.getElementById("value1").value = Math.floor(89+Math.random()*11); 
     document.getElementById("value2").value = Math.floor(89+Math.random()*11);​ 
    </script> 

を:http://jsfiddle.net/wVAFt/

+0

これはうまくいった!ありがとうございました:) – Gcap

+0

唯一のことは答え変数がnum1になり、num2が一緒に打ち砕かれました。時々私はプログラミングが嫌いです-_- – Gcap

+0

一緒に打ち砕かれたのはどういう意味ですか? – adedoy

0
<input type="text" id="answer" value="5" > 

or 

document.getElementById("answer").value = "5"; 
+0

ありがとうございますが、問題は変数の値をフォームに書きたいということです。私はsubmitを押してからbackボタンを押して、テキストボックスに変数の値を表示しなければならないことを除いて、変数名に5を置き換えて、私のために働いた – Gcap

+0

フォームを送信せずにトリガーするボタンを作ることができます更新のいくつかの並べ替え! –

0

入力フィールドに数値を設定するには、value="10"のような値を設定できます。デフォルト値は、ユーザーが変更するまで10になります。

関連する問題