HTML display result in text (input) field?

后端 未结 4 1732
予麋鹿
予麋鹿 2021-01-01 03:08

I have a simple HTML form, with 3 input field, when you hit = button, it suppose to add 2 numbers that you typed in the first 2 input fields and display result in 3rd filed.

相关标签:
4条回答
  • 2021-01-01 03:43
     <HTML>
          <HEAD>
            <TITLE>Sum</TITLE>
    
            <script type="text/javascript">
              function sum()
              {
    
                 var num1 = document.myform.number1.value;
                 var num2 = document.myform.number2.value;
                 var sum = parseInt(num1) + parseInt(num2);
                 document.getElementById('add').value = sum;
              }
            </script>
          </HEAD>
    
          <BODY>
            <FORM NAME="myform">
              <INPUT TYPE="text" NAME="number1" VALUE=""/> + 
              <INPUT TYPE="text" NAME="number2" VALUE=""/>
              <INPUT TYPE="button" NAME="button" Value="=" onClick="sum()"/>
              <INPUT TYPE="text" ID="add" NAME="result" VALUE=""/>
            </FORM>
    
          </BODY>
    </HTML>
    

    This should work properly. 1. use .value instead of "innerHTML" when setting the 3rd field (input field) 2. Close the input tags

    0 讨论(0)
  • 2021-01-01 03:59

    With .value and INPUT tag

    <HTML>
      <HEAD>
        <TITLE>Sum</TITLE>
    
        <script type="text/javascript">
          function sum()
          {
    
             var num1 = document.myform.number1.value;
             var num2 = document.myform.number2.value;
             var sum = parseInt(num1) + parseInt(num2);
             document.getElementById('add').value = sum;
          }
        </script>
      </HEAD>
    
      <BODY>
        <FORM NAME="myform">
          <INPUT TYPE="text" NAME="number1" VALUE=""/> + 
          <INPUT TYPE="text" NAME="number2" VALUE=""/>
          <INPUT TYPE="button" NAME="button" Value="=" onClick="sum()"/>
          <INPUT TYPE="text" ID="add" NAME="result" VALUE=""/>
        </FORM>
    
      </BODY>
    </HTML>
    

    with innerHTML and DIV

    <HTML>
      <HEAD>
        <TITLE>Sum</TITLE>
    
        <script type="text/javascript">
          function sum()
          {
    
             var num1 = document.myform.number1.value;
             var num2 = document.myform.number2.value;
             var sum = parseInt(num1) + parseInt(num2);
             document.getElementById('add').innerHTML = sum;
          }
        </script>
      </HEAD>
    
      <BODY>
        <FORM NAME="myform">
          <INPUT TYPE="text" NAME="number1" VALUE=""/> + 
          <INPUT TYPE="text" NAME="number2" VALUE=""/>
          <INPUT TYPE="button" NAME="button" Value="=" onClick="sum()"/>
          <DIV  ID="add"></DIV>
        </FORM>
    
      </BODY>
    </HTML>
    
    0 讨论(0)
  • 2021-01-01 04:01

    innerHTML sets the text (including html elements) inside an element. Normally we use it for elements like div, span etc to insert other html elements inside it.

    For your case you want to set the value of an input element. So you should use the value attribute.

    Change innerHTML to value

    document.getElementById('add').value = sum;
    
    0 讨论(0)
  • 2021-01-01 04:02

    Do you really want the result to come up in an input box? If not, consider a table with borders set to other than transparent and use

    document.getElementById('sum').innerHTML = sum;

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