0

テキストボックスに乱数を入力しようとしています現在、1つのテキストボックスで試しています。イベントの読み込み時に、javascript を使用してテキスト ボックスに乱数を入力します。

<script type="text/javascript">
    function Random() 
      {
        return Math.floor(Math.random() * 10);
      } 
</script>   

<form name="f1">
    a=      <input type="text" name="field1" /></br>
    b=      <input type="text" name="field2" /></br>
    Answer: <input type="text" name="ansfield" />

<input type="button" value="Fill"  onload()="document.getElementById('field1').value=Random()"/>

</form>
4

2 に答える 2

0

HTML<script>の末尾に を配置し、その中にフィールド値を設定します。idのプロパティも欠落していますinput:

<body>
    <form name="f1">
        a=      <input type="text" id="field1" name="field1" /></br>
        b=      <input type="text" name="field2" /></br>
        Answer: <input type="text" name="ansfield" />

        <input type="button" value="Fill" />
    </form>
    <script type="text/javascript">
        function Random() {
            return Math.floor(Math.random() * 10);
        }

        document.getElementById('field1').value = Random()
    </script>
</body>
于 2014-10-06T13:39:13.970 に答える