2014-10-02 16 views
2

我有這個HTML和Java腳本。我設置爲自動顯示這3個輸入字段的總金額。但是如果我使用+(添加),它會給我錯誤的答案。如果我使用*(乘)或/(分開),我工作得很好。任何人請幫助!在HTML中添加數字值

這是我的HTML代碼。

</pre> 
    <tr> 
     <td><input type='number' id='amt1' name='amt1' onkeyup='total_amount();' onKeyUp='return numbersonly(event);' class='form-control'></td> 
     <td><input type='number' id='amt2' name='amt2' onkeyup='total_amount();' onKeyUp='return numbersonly(event);' class='form-control'></td> 
     <td><input type='number' id='amt3' name='amt3' onkeyup='total_amount();' onKeyUp='return numbersonly(event);' class='form-control'></td> 
     <td><input type='number' id='total' name='total' class='form-control' readonly='readonly'></td> 
    </tr> 
<pre> 

我的繼承人Java腳本...

<script type="text/javascript"> 

    function total_amount() 
    { 
     document.getElementById('total').value = document.getElementById('amt1').value + document.getElementById('amt2').value + document.getElementById('amt3').value 
    } 

    function numbersonly(e){ 
     var unicode=e.charCode? e.charCode : e.keyCode 
     if (unicode!=8 && unicode!=46 && unicode!=37 && unicode!=27 && unicode!=38 && unicode!=39 && unicode!=40 && unicode!=9){ //if the key isn't the backspace key (which we should allow) 
      if (unicode<48||unicode>57) 
       return false 
     } 
    } 

</script> 
</pre> 
+0

哪裏是你的HTML嗎? – 2014-10-02 06:11:16

+0

爲什麼'「1」+「1」==「11」'?哦,你正在添加字符串... – RobG 2014-10-02 06:14:50

+1

限制用戶對某些鍵的輸入不是用戶友好的,並且不會通過粘貼或其他方式停止輸入非數字,只是不要這樣做。您只關心用戶完成輸入後的價值,他們如何到達那裏取決於他們。 – RobG 2014-10-02 06:25:13

回答

1

使用

document.getElementById('amt1').value|0 + 
document.getElementById('amt2').value|0 + 
document.getElementById('amt3').value|0 ; 

OR

parseInt(document.getElementById('amt1').value) + 
parseInt(document.getElementById('amt2').value) + 
parseInt(document.getElementById('amt3').value) ; 

,使這些被視爲數量

+1

僅限代碼解答不被接受,請解釋OP問題以及您的答案爲何解決此問題。如果OP使用浮點數,'| 0'不是一個好策略。 – RobG 2014-10-02 06:21:45

+0

應該使用parseInt。但它不允許文本框爲空。你應該輸入0. – 2014-10-02 07:04:25

+0

然後把空的文本框的檢查。如果文本框爲空,則使用0或不解析字符串。 – Prince 2014-10-02 07:08:25

1

更改功能

function total_amount() { 
    var amt1 = parseInt(document.getElementById('amt1').value); 
    if (isNaN(amt1)) { 
     amt1 = 0; 
    } 

    var amt2 = parseInt(document.getElementById('amt2').value); 
    if (isNaN(amt2)) { 
     amt2 = 0; 
    } 

    var amt3 = parseInt(document.getElementById('amt3').value); 
    if (isNaN(amt3)) { 
     amt3 = 0; 
    } 

    document.getElementById('total').value = amt1 + amt2 + amt3; 
} 
+0

謝謝!這有助於很多。比別忘記要正確的標記 – 2014-10-02 06:52:11

+0

。所以它會幫助其他人。 – 2014-10-02 06:56:10

+0

仍然連接。 – 2014-10-02 07:06:19

1

使用parseInt()

function total_amount() 
{ 
    document.getElementById('total').value =parseInt(document.getElementById('amt1').value) + 
parseInt(document.getElementById('amt2').value) + parseInt(document.getElementById('amt3').value) 
} 
1

爲一個字符串(由.value的返回)轉換爲數字的簡單解決方案是使用一元+操作者:

function total_amount() { 
    document.getElementById('total').value = 
     +document.getElementById('amt1').value + 
     +document.getElementById('amt2').value + 
     +document.getElementById('amt3').value 
} 
2

保持簡單。使用event delegation和一個keyUp處理程序。不要使用內聯處理程序。在對它進行計算之前,將字段值轉換爲Number。這裏有一個例子:

document.querySelector('table').addEventListener('keyup', sum); 
 

 
function sum(e) { 
 
    var from = e.target || e.srcElement 
 
    ,isNumericInput = /number/i.test(from.type); 
 
    if (!isNumericInput) {return true;} 
 
    var d = document 
 
    ,inputs = d.querySelector('table').querySelectorAll('[type=number]') 
 
    ,sumfld = d.querySelector('#sum') 
 
    ,sumnow = 0; 
 
    
 
    [].forEach.call(inputs, 
 
        function (v) { 
 
         sumnow += +(v.value); //<= conversion to Number, using + 
 
        }); 
 
    
 
    sumfld.textContent = sumnow; 
 
}
<table> 
 
    <tr> 
 
    <td><input type='number' id='amt1' name='amt1' class='form-control'></td> 
 
    <td><input type='number' id='amt2' name='amt2' class='form-control'></td> 
 
    <td><input type='number' id='amt3' name='amt3' class='form-control'></td> 
 
    <td><b>Total</b>: <span id="sum"></span></td> 
 
    </tr> 
 
</table>

+0

我應該在哪裏放置這些? ('table')。addEventListener('keyup',sum);' 這些應該包含在我的腳本函數中嗎? – 2014-10-02 07:26:10

+0

@JohnArzaga在腳本的頂部,或者在'onload'處理器 – KooiInc 2014-10-02 07:27:58

+0

@RobG,當然。我不會說,但是刪除了'|| 0'部分。 – KooiInc 2014-10-02 07:30:09