添加两个数字并使用Javascript在文本框中显示结果

时间:2015-02-14 20:49:59

标签: javascript html5 function textbox

我只是想编写一个简单的javascript程序,它将演示从文本字段中获取用户输入,然后单击该按钮将在另一个文本字段中显示这些数字的总和结果。但不幸的是,以下代码无效。这样做我完全无能为力。单击该按钮不会在结果文本字段中显示任何内容。

请帮忙。

<body>
    <div>
        <div>
            <h1>Add two number using text box as input using javascript</h1>
        </div>
            Enter First Number : <br>
            <input type="text" id="Text1" name="TextBox1">
            <br>
            Enter Second Number : <br>
            <input type="text" id="Text2" name="TextBox2">
            <br>
            Result : <br>
            <input type="text" id="txtresult" name="TextBox3">
            <br>
         <input type="button" name="clickbtn" value="Display Result" onclick="add_number()">

        <script type="text/javascript">
            function add_number(){
            var first_number = parseInt(document.getElementsById("Text1").value);
            var second_number = parseInt(document.getElementsById("Text2").value);
            var result = first_number + second_number;
            document.getElementById("txtresult").innerHTML = result;    
            }
        </script>

10 个答案:

答案 0 :(得分:3)

这是一个工作小提琴:http://jsfiddle.net/sjh36otu/

        function add_number() {

            var first_number = parseInt(document.getElementById("Text1").value);
            var second_number = parseInt(document.getElementById("Text2").value);
            var result = first_number + second_number;

            document.getElementById("txtresult").value = result;
        }

答案 1 :(得分:2)

分配变量&#34; first_number&#34;和&#34; second_number&#34;,你需要改变&#34; document.getElementsById&#34;单数&#34; document.getElementById&#34;。

答案 2 :(得分:1)

var first_number = parseInt(document.getElementById("Text1").value);
var second_number = parseInt(document.getElementById("Text2").value);

// This is because your method .getElementById has the letter 's': .getElement**s**ById

答案 3 :(得分:0)

<script>

function sum()
{
    var value1= parseInt(document.getElementById("txtfirst").value);
    var value2=parseInt(document.getElementById("txtsecond").value);
    var sum=value1+value2;
    document.getElementById("result").value=sum;

}
 </script>

答案 4 :(得分:0)

你犯了一个简单的错误。别担心.... 只需使用getElementById代替getElementsById

<强>真

var first_number = parseInt(document.getElementById("Text1").value);

<强>假

var first_number = parseInt(document.getElementsById("Text1").value);

谢谢...

答案 5 :(得分:0)

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {

    $scope.minus = function() {     

	    var a = Number($scope.a || 0);
            var b = Number($scope.b || 0);
            $scope.sum1 = a-b;
	   // $scope.sum = $scope.sum1+1; 
	   alert($scope.sum1);
    }

   $scope.add = function() {     

	    var c = Number($scope.c || 0);
            var d = Number($scope.d || 0);
            $scope.sum2 = c+d;
	   alert($scope.sum2);
    }
});
<head>
      <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular.min.js"></script>
   </head>
<body>

<div ng-app="myApp" ng-controller="myCtrl">
     <h3>Using Double Negation</h3>

    <p>First Number:
        <input type="text" ng-model="a" />
    </p>
    <p>Second Number:
        <input type="text" ng-model="b" />
    </p>
    <button id="minus" ng-click="minus()">Minus</button>
    <!-- <p>Sum: {{ a - b }}</p>  -->
 <p>Sum: {{ sum1 }}</p>

    <p>First Number:
        <input type="number" ng-model="c" />
    </p>
    <p>Second Number:
        <input type="number" ng-model="d" />
    </p>
 <button id="minus" ng-click="add()">Add</button>
    <p>Sum: {{ sum2 }}</p>
</div>

答案 6 :(得分:0)

<script>
function myFunction() {
var y = parseInt(document.getElementById("txt1").value);
var z = parseInt(document.getElementById("txt2").value);
var x = y + z;
document.getElementById("result").innerHTML = x;
}
</script>
<p>
<label>Enter First Number : </label><br>
<input type="number" id="txt1" name="text1"><br/>
<label>Enter Second Number : </label><br>
<input type="number" id="txt2" name="text2">
</p>
<p>
<button onclick="myFunction()">Calculate</button>
</p>
<br/>
<p id="result"></p>

答案 7 :(得分:0)

应该是document.getElementById("txtresult").value= result;

您正在将文本框的值设置为结果。 id="txtresult" 不是 HTML 元素。

答案 8 :(得分:0)

<script>
       var text1 = document.getElementById("Text1").value;
       var text2 = document.getElementById("Text2").value;
       var answer = parseInt(text1) + parseInt(text2);
      function add_number(){
       document.getElementById("txtresult").value = answer;
       }
</script>

答案 9 :(得分:-1)

您可以使用JavaScript中的Number原语类型简单地转换给定数字,如下所示。

df$knownForTitles <- sapply(strsplit(df$knownForTitles, ','), function(x) 
                 with(df, toString(na.omit(primaryTitle[match(x, tconst)]))))