如何获得输入值并总计总和

时间:2016-08-17 05:30:46

标签: javascript jquery

我想尝试这样做,我想使用javascript获取输入字段中的所有数据,并将所有字段的总和放在另一个输入中。但是我得到一个错误,总的值是null或空白。有人可以帮我这个吗?或者告诉我导致错误的原因。

这里输入的html代码我不会包含减少代码的所有字段。



  var ma = +(document.getElementById('majore').textContent);
			  var qu = +(document.getElementById('quize').textContent);
			  var hss = +(document.getElementById('hs').textContent);
			  var attt = +(document.getElementById('att').textContent);
			  var laboo = +(document.getElementById('labo').textContent);
 			  var acttt = +(document.getElementById('act').textContent);
 			  var recitss = +(document.getElementById('recits').textContent);

 			  var total = ma + qu + hss + attt + laboo + acttt + recitss;

			  document.getElementById('totals').innerHTML = total;

<div class="well me">
				 <label for="majore">Major Exam</label>
		            <div class="input-group">
					  <input type="text" class="form-control majore" id="majore"/>
					   <span class="input-group-addon">
			           <i class="fa fa-percent"></i>
				       </span>
				</div>
			</div>

            <div class="well quize">
				 <label for="quize">Quizzes</label>
		            <div class="input-group">
					  <input type="text" class="form-control quize" id="quize"/>
					   <span class="input-group-addon">
			           <i class="fa fa-percent"></i>
				       </span>
				</div>
			</div>

            <div class="well hos">
				 <label for="hs">Homework/Seatwork</label>
		            <div class="input-group">
					  <input type="text" class="form-control hs" id="hs"/>
					   <span class="input-group-addon">
			           <i class="fa fa-percent"></i>
				       </span>
				</div>
			</div>


              <div class="well total">
				 <label for="total">Total/label>
		            <div class="input-group">
					  <input type="text" class="form-control total" id="totals"/>
					   <span class="input-group-addon">
			           <i class="fa fa-percent"></i>
				       </span>
				</div>
			</div>


here's my js. this code is inside in the `success function` of `ajax`. 
&#13;
&#13;
&#13;

4 个答案:

答案 0 :(得分:2)

使用value属性,而不是textContent/innerHTML从[{1}}获取value

另请注意,您正在访问Input-Element以外的少数元素,我已从演示中删除了这些语句

DOM
var ma = +(document.getElementById('majore').value);
var qu = +(document.getElementById('quize').value);
var hss = +(document.getElementById('hs').value);
var total = ma + qu + hss;
document.getElementById('totals').value = total;

答案 1 :(得分:1)

function numberSum(N) {
 var total = 0;
  for(var i = 1; i <= N; i++){
  total += i;
  }
  return total;

}

function run(){
val = document.getElementById("val").value;
document.getElementById("results").innerHTML=val+": "+numberSum(val)
}
<input id="val">
<input type="Submit" onclick="run();">
 <p id="results"></p>

你只展示你的身体部位......好像这样做这个剧本

答案 2 :(得分:1)

您可以尝试使用此代码

<div class="well me">
<label for="majore">Major Exam</label>
<div class="input-group">
    <input type="text" class="form-control majore" id="majore"  onchange="sum()"/>
    <span class="input-group-addon">
        <i class="fa fa-percent"></i>
    </span>
</div>

<div class="well quize">
<label for="quize">Quizzes</label>
<div class="input-group">
    <input type="text" class="form-control quize" id="quize" onchange="sum()"/>
    <span class="input-group-addon">
        <i class="fa fa-percent"></i>
    </span>
</div>

<div class="well hos">
<label for="hs">Homework/Seatwork</label>
<div class="input-group">
    <input type="text" class="form-control hs" id="hs" onchange="sum()"/>
    <span class="input-group-addon">
        <i class="fa fa-percent"></i>
    </span>
</div>

<div class="well total">
<label for="total">Total</label>
<div class="input-group" id="totals">
        <input type="text" class="form-control total" id="totals"/>
        <span class="input-group-addon">
            <i class="fa fa-percent"></i>
        </span>
    </div>

<script>
function sum() {
    var ma = parseInt(document.getElementById('majore').value);
    var qu = parseInt(document.getElementById('quize').value);
    var hss = parseInt(document.getElementById('hs').value);
    var totals = ma + qu + hss;
    document.getElementById('totals').innerHTML = totals;
}</script>

答案 3 :(得分:1)

您可以使用$scope.validateFunction = function() { angular.forEach($scope.list, function(value, key){ var IsInList = false; if($("#textbox").val() == value.listvalue) { IsInList = true; break; } }); return IsInList; } //Invoke the function like follow: $scope.validateFunction(); 属性来获取或设置输入元素的值。

  • 从input元素中获取值并将值相加
  • 设置总输入字段的值

以下是代码:

val

检查Jsfiddle

中的输出

这是一种简单而优化的方式。详细了解var total = +($('#majore').val()) + +($('#quize').val()) + +($('#hs').val()); $('#totals').val(total); 属性here