简体   繁体   中英

Calculating two fields in a form using JavaScript with a submit button and reset button

Here the code that I made and not working looking to see how I would get my sum into the result field, Also looking how to make a reset button to clear all the text from the fields.

JSBin .

<html>

<head>
    <script>
        function addition() {
            var x = +document.getElementById('num1').value;
            var y = +document.getElementById('num2').value;
            document.getElementById('result').value = x + y;
            document.result.submit()
        }

        function reset() {}
    </script>
</head>

<body>
    <header>
        <h1>Calculator Using Forms &amp; JavaScript</h1>
    </header>

    <form>
        Number 1:
        <input type="number" id="num1" value="0" autofocus>
        <br> Number 2:
        <input type="number" id="num2" value="0">
        <br> Result:
        <input type="text" id="result" value="0" readonly>
        <br>
    </form>

    <input type="button" onclick="addition()" value="addition">
</body>

</html>

Why do you want to submit the form?

You can use document.forms. to access the form.

Try this:

<html>

<head>
    <script>
        function addition() {
            var x = +document.getElementById('num1').value;
            var y = +document.getElementById('num2').value;
            document.getElementById('result').value = x + y;
            document.forms.calculator.submit()
        }

        function reset() {
          document.forms.calculator.reset();
        }
    </script>
</head>

<body>
    <header>
        <h1>Calculator Using Forms &amp; JavaScript</h1>
    </header>

    <form name="calculator">
        Number 1:
        <input type="number" id="num1" value="0" autofocus>
        <br> Number 2:
        <input type="number" id="num2" value="0">
        <br> Result:
        <input type="text" id="result" value="0" readonly>
        <br>
    </form>

    <input type="button" onclick="addition()" value="addition">
    <input type="button" onclick="reset()" value="reset">
</body>

</html>

just give an id to the form and in your reset function, just do

document.getElementById("myForm").reset();

updates:

according to ROBG's comments, you don't need to bind a function to the button. you can simply add a

<input type="reset">

to reset all fields of the form, and if the button is out the form, you can do <input form="formID" type="reset"> to associate to specific form.

Do you want this one? jsbin

<html>
<head>
<script src="https://code.jquery.com/jquery-3.0.0-alpha1.js"></script>
    <script>
        function addition() {
            var x = +document.getElementById('num1').value;
            var y = +document.getElementById('num2').value;
            document.getElementById('result').value = x + y;
            //document.result.submit()

            //if you want use submit, use this code. if only see the result, don't use this
            //Submit is receive data, so you can't see a result.
            //document.getElementById('formId').submit();

            //jquery submit
            //$("form")[0].submit();
        }

        function reset() {
          //no jquery
          document.getElementById('formId').reset();
          //jquery
          //$("form")[0].reset();
        }
    </script>
</head>
<body>
    <header>
        <h1>Calculator Using Forms &amp; JavaScript</h1>
    </header>
    <form id="formId">
        Number 1:
        <input type="number" id="num1" value="0" autofocus>
        <br> Number 2:
        <input type="number" id="num2" value="0">
        <br> Result:
        <input type="text" id="result" value="0" readonly>
        <br>
    </form>

    <input type="button" onclick="addition()" value="addition">
    <input type="button" onclick="reset()" value="reset">
</body>

</html>

Note that forms can be submitted without clicking the submit button, so if you want something to happen on submit, attach a listener to the form's submit handler. To reset a form only requires a reset button, no code.

Form controls must have a name to be successful and submit their value with the form, and you can pass a reference to the form using this from the listener so you don't need to use getElementById .

Implementing the above significantly reduces the amount of code required.

In the following, the submit listener returns false so prevents the form from submitting. If you want the form to submit, just remove the return statement from the in–line listener:

 function addition(form) { form.result.value = +form.num1.value + +form.num2.value; }
 <form name="calculator" onsubmit="addition(this); return false;"> Number 1: <input type="number" name="num1" value="0" autofocus> <br> Number 2: <input type="number" name="num2" value="0"> <br> Result: <input type="text" name="result" value="0" readonly> <br> <input type="submit" value="Submit form"> <input type="reset"> </form>

You may not want to submit the form at all, in that case you can make the submit button a plain button and call the function from there, but remember to stop the form submitting otherwise.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM