JavaScript - 从脚本中将值写入html表单

Gca*_*cap 5 html javascript forms

我想知道如何将值输入到带有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>
Run Code Online (Sandbox Code Playgroud)

我感谢任何帮助!谢谢!

ade*_*doy 4

您可以在创建表单后放置此脚本:

\n\n
<form name="addition" action="" method="get">\n            <table border="1">\n                     <tr>\n                 <td>Value 1:</td> <td><input type="text" name="value1" id="value1"/></td>\n                         </tr>\n                     <tr>\n                 <td>Value 2:</td> <td><input type="text" name="value2" id="value2"/></td>\n                         </tr>\n                     <tr>\n                 <td>Answer:</td> <td><input type="text" id="answer" /></td>\n                         </tr>\n                     <tr><td><input type="button" value="Submit" onClick="addSubmit()" id="submit" /></td><td><input type="button" value="Give Up" onClick="addGiveUp()" /></td></tr>\n                     </table>\n</form>\n\n<!-- This is the script-->\n <script type="text/javascript">\n      document.getElementById("value1").value = Math.floor(89+Math.random()*11);\n      document.getElementById("value2").value = Math.floor(89+Math.random()*11);\xe2\x80\x8b\n  </script>\n
Run Code Online (Sandbox Code Playgroud)\n\n

这将为您生成随机数并将它们放入表单\n请参阅此处的代码: http: //jsfiddle.net/wVAFt/

\n