admin管理员组

文章数量:1401959

I have code where I need to get a random number when I click the submit button. The random number should be displayed in the same page in another text box.

This is my code:

<html>
<head>
<script type="text/javascript">
function randomnumber(num1, num2)
{
    num1 = parseInt(num1);
    num2 = parseInt(num2);
    if(num1 >= num2)
    {
        alert("Number 2 should be greater than Number 1");
    }
    else
    {
        var generator = Math.random()*(num2-num1);
        generator = Math.round(num1+generator);

        document.test.result.value = generator;
        document.getElementById("p1").innerHTML=generator;
    }
}
</script>
</head>
<body>
    <form name="test" onsubmit = "return randomnumber(num1,num2)" >
        <table>
            <tr>
                <td>
                    <input type="text" name="num1" size ="35" maxlength= "25">
                </td>
                <td>
                    <input type="text" name="num2" size ="35" maxlength= "25">
                </td>
                <td>
                    <input type="text" name="p1" value = "" size ="35" maxlength= "25">
                </td>
                <td>
                    <input type="submit" name="submit" value"generate">
                </td>
            </tr>
        </table>
    </form>
</body>
</html>

The random number is generated in the variable 'generator' in the script. I need to get the value in this textfield.

<td>
    <input type="text" name="p1" value = "" size ="35" maxlength= "25">
</td>

I have code where I need to get a random number when I click the submit button. The random number should be displayed in the same page in another text box.

This is my code:

<html>
<head>
<script type="text/javascript">
function randomnumber(num1, num2)
{
    num1 = parseInt(num1);
    num2 = parseInt(num2);
    if(num1 >= num2)
    {
        alert("Number 2 should be greater than Number 1");
    }
    else
    {
        var generator = Math.random()*(num2-num1);
        generator = Math.round(num1+generator);

        document.test.result.value = generator;
        document.getElementById("p1").innerHTML=generator;
    }
}
</script>
</head>
<body>
    <form name="test" onsubmit = "return randomnumber(num1,num2)" >
        <table>
            <tr>
                <td>
                    <input type="text" name="num1" size ="35" maxlength= "25">
                </td>
                <td>
                    <input type="text" name="num2" size ="35" maxlength= "25">
                </td>
                <td>
                    <input type="text" name="p1" value = "" size ="35" maxlength= "25">
                </td>
                <td>
                    <input type="submit" name="submit" value"generate">
                </td>
            </tr>
        </table>
    </form>
</body>
</html>

The random number is generated in the variable 'generator' in the script. I need to get the value in this textfield.

<td>
    <input type="text" name="p1" value = "" size ="35" maxlength= "25">
</td>
Share Improve this question edited Oct 10, 2012 at 7:39 jonsca 10.4k26 gold badges56 silver badges63 bronze badges asked Oct 10, 2012 at 7:33 uday kurumanauday kurumana 171 gold badge1 silver badge3 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 1

What you want is:

var generator = Math.random()*(num2-num1);
generator = Math.round(num1+generator);

// document.test.result.value = generator; // Not sure what this line is for (don't see a test.result element.
document.getElementById("p1").value=generator;

Note .value instead of .innerHTML.

This should do it

document.getElementsByName('p1')[0].value = generator

The issue is "p1" is not the id of the textbox but the name. The above code will only work for the case of one element with the name "p1".

The following will work only if you use "p1" as the Id of your textbox.

document.getElementById('p1').value = generator

Also, for textboxes, use .value instead of .innerHTML

Is better in your html to change your input text in this mode:

<input type="text" name="p1" id="p1" value = "" size ="35" maxlength= "25">

and use this javascript code to change value:

document.getElementById("p1").value=generator;

Because the id is unique and you can select better your element

There are multiple issues with the code (as well as the intended functionality, which looks like a homework assignment). To put text in the input field, it is best to use an id attribute in the element, id="p1" in this case, and assign to document.getElementById('p1').value. Do not use form submission, since you are not submitting data to a server-side form handler. Instead, use <input type="button"> and an onclick event handler in it. Check the input values for being numeric, at least by testing the result of parseInt(), before doing any parisons on them. Remove the assignment to document.test.result.value, as its an erroneous reference and aborts the execution of the rest of the code. And check your HTML markup with a markup validator.

what is result

document.test.result.value=generator;

try name of the field p1.

document.test.p1.value=generator;

本文标签: javascriptDisplay random number in a textbox field in the same HTML pageStack Overflow