我写这个函数有什么问题?

时间:2017-08-11 03:23:09

标签: javascript html function

为什么我的功能不起作用?我希望输入到输入字段中的数字自动添加到"下部总计"框。

      3 of a kind: <input type="text" name="qtyB">
      <br>
      4 of a kind: <input type="text" name="qtyB">
      <br>
      Full House: <input type="text" name="qtyB">
      <br>
      Little Straight: <input type="text" name="qtyB">
      <br>
      Lg. Straight: <input type="text" name="qtyB">
      <br>
      <b>Yahtzeebeshy:</b> <input type="text" name="qtyB">
      <br>
      Chance: <input type="text" name="qtyB">
      <br>
      Lower Section Total: <input type="text" id="LowerSectionTotal">

function findTotalB() {
  var arrB = document.getElementsByName('qtyB');
  var totB = 0;
  for (var i = 0; i < arrB.length; i++) {
    if (parseInt(arrB[i].value))
      totB += parseInt(arrB[i].value);
  }

  document.getElementById('LowerSectionTotal').value = totB;

}

5 个答案:

答案 0 :(得分:0)

function findTotalB() {
  var arrB = document.getElementsByName('qtyB');
  var totB = 0;

  for (var i = 0; i < arrB.length; i++) {
     var value = 0 ;

     try {
        value = parseInt(arrB[i].value.trim());
     } catch ( e ) {
     }

     totB += value ; 
  }

  document.getElementById('LowerSectionTotal').value = totB;

}

答案 1 :(得分:0)

你的功能非常好。你可以在下面的小提琴中看到。我已将其设置为每秒运行一次,您可以看到它在您在其他输入中键入值时更新总数。

现在您想要的是将文本change event绑定到源输入,并在其值更改时调用findTotalB函数。

&#13;
&#13;
function findTotalB() {
  var arrB = document.getElementsByName('qtyB');
  var totB = 0;
  for (var i = 0; i < arrB.length; i++) {
    if (parseInt(arrB[i].value))
      totB += parseInt(arrB[i].value);
  }

  document.getElementById('LowerSectionTotal').value = totB;
}

setInterval(findTotalB, 1000);
&#13;
3 of a kind: <input type="text" name="qtyB">
<br> 4 of a kind: <input type="text" name="qtyB">
<br> Full House: <input type="text" name="qtyB">
<br> Little Straight: <input type="text" name="qtyB">
<br> Lg. Straight: <input type="text" name="qtyB">
<br>
<b>Yahtzeebeshy:</b> <input type="text" name="qtyB">
<br> Chance: <input type="text" name="qtyB">
<br> Lower Section Total: <input type="text" id="LowerSectionTotal">
&#13;
&#13;
&#13;

答案 2 :(得分:0)

试试这个:

var arrB = document.getElementsByName('qtyB');
function findTotalB() {
  
  var totB = 0;
  for (var i = 0; i < arrB.length; i++) {
    if (parseInt(arrB[i].value))
      totB += parseInt(arrB[i].value);
  }

  document.getElementById('LowerSectionTotal').value = totB;

}
for(var i = 0; i < arrB.length; i++) {
	arrB[i].onkeyup = findTotalB;	
}	
3 of a kind: <input type="text" name="qtyB">
      <br>
      4 of a kind: <input type="text" name="qtyB">
      <br>
      Full House: <input type="text" name="qtyB">
      <br>
      Little Straight: <input type="text" name="qtyB">
      <br>
      Lg. Straight: <input type="text" name="qtyB">
      <br>
      <b>Yahtzeebeshy:</b> <input type="text" name="qtyB">
      <br>
      Chance: <input type="text" name="qtyB">
      <br>
      Lower Section Total: <input type="text" id="LowerSectionTotal">

您的功能很好,代码只是在输入qtB后才会收听,如果是,则会调用您的函数fundTotalB

答案 3 :(得分:0)

&#13;
&#13;
function findTotalB() {
  var arrB = document.getElementsByName('qtyB');
  var totB = 0;
  for (var i = 0; i < arrB.length; i++) {
    if (parseInt(arrB[i].value))
      totB += parseInt(arrB[i].value);
  }

  document.getElementById('LowerSectionTotal').value = totB;

}
&#13;
  3 of a kind: <input type="text" name="qtyB">
      <br>
      4 of a kind: <input type="text" name="qtyB">
      <br>
      Full House: <input type="text" name="qtyB">
      <br>
      Little Straight: <input type="text" name="qtyB">
      <br>
      Lg. Straight: <input type="text" name="qtyB">
      <br>
      <b>Yahtzeebeshy:</b> <input type="text" name="qtyB">
      <br>
      Chance: <input type="text" name="qtyB">
      <br>
      Lower Section Total: <input type="text" id="LowerSectionTotal">

<button onClick="findTotalB()" > Click </button>
&#13;
&#13;
&#13;

您可以添加功能触发器。或者您可以添加所有输入onchange="findTotalB()"。如果任何输入发生变化,则触发,自动运行。

<input onchange="findTotalB()" type="text" name="qtyB">

答案 4 :(得分:0)

如果您想自动更新到var string 1 = ['7','7.5','8','8.5','9','9.5','10','10.5','11','11.5','12','13','14']; var string 2 = ['Boot','Casual','Dual Comfort','Plain Toe'] 框,请将Lower Section Total绑定到findTotalB()事件。输入和keydown框中的用户类型将自动更新

&#13;
&#13;
Lower Section Total
&#13;
function findTotalB() {
  var arrB = document.getElementsByName('qtyB');
  var totB = 0;
  for (var i = 0; i < arrB.length; i++) {
    if (parseInt(arrB[i].value))
      totB += parseInt(arrB[i].value);
  }

  document.getElementById('LowerSectionTotal').value = totB;

}

// Binding 'keydown' event
var arrB = document.getElementsByName('qtyB');
var ln = arrB.length;
for (var i = 0; i < ln; i++) {
  document.getElementsByName('qtyB')[i].addEventListener("keydown", findTotalB, false);
}
&#13;
&#13;
&#13;