JavaScript - writing value into html form from script

后端 未结 3 1215
Happy的楠姐
Happy的楠姐 2021-01-19 03:00

I was wondering how I could input a value into a form with html. So right when the user goes to the site there\'s numbers in the text boxes already and they use those number

相关标签:
3条回答
  • 2021-01-19 03:27

    To have numbers in input fields you can set them to have a value, like value="10" and the default value would be 10 until the user changes it.

    0 讨论(0)
  • 2021-01-19 03:34
    <input type="text" id="answer" value="5" >
    
    or
    
    document.getElementById("answer").value = "5";
    
    0 讨论(0)
  • 2021-01-19 03:40

    well you can put this script after the form is created:

    <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>
    

    That would generate random numbers for you and put them in the form See here for the code: http://jsfiddle.net/wVAFt/

    0 讨论(0)
提交回复
热议问题