2017-08-11 73 views
1

爲什麼我的功能不起作用?我希望放入輸入字段的數字自動添加到「下部分總計」框中。我寫的這個函數有什麼問題?

 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; 

} 
+4

表示不會調用函數的代碼。 – nnnnnn

+0

這是接近它的另一種方式:https://jsfiddle.net/sheriffderek/h2f2r45j/ – sheriffderek

回答

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; 

} 
0

你的功能是完全正常的。你可以在下面的小提琴中看到。我已經將它設置爲每秒運行一次,並且您可以看到它隨着您在其他輸入中鍵入值而更新總數。

現在您想要將文本change event綁定到源輸入,並在其值更改時調用您的findTotalB函數。

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);
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">

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++) { 
 
\t arrB[i].onkeyup = findTotalB; \t 
 
} \t
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將被調用。

0

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; 
 

 
}
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>

您可以添加功能觸發。或者您可以添加所有輸入onchange="findTotalB()"。如果有任何輸入會改變,則自動執行功能處理。

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

如果你想自動更新到Lower Section Total框,然後綁定findTotalB()到​​事件。當輸入和Lower Section Total框用戶類型將得到自動更新

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); 
 
}
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">