簡體   English   中英

使用jQuery的輸入框中的值總和不起作用

[英]sum of values in input boxes using jquery not working

我正在創建一個從哈希中獲取值表單,並在我的情況下為哈希值和字符串床的每個鍵名屬性組合創建三個輸入框(具有金額,acNo,debNo),現在我想獲取輸入框中名稱中帶有“ bed”且類別為“ amount”的值。

 $(document).ready(function(){
 alert('document is loaded');
    $("input[name*='bed'][class='amount']").each(function() {//doen't go in this loop

        alert('in the bed'); //no alert
            $(this).keyup(function(){

                calculateSum();
            });
        });

    });

    function calculateSum() {

        var sum = 0;
        //iterate through each textboxes and add the values
        $("input[name*='bed'][class='amount']").each(function() { 
            //add only if the value is number
            if(!isNaN(this.value) && this.value.length!=0) {
                sum += parseFloat(this.value);
            }

        });
        //.toFixed() method will roundoff the final sum to 2 decimal places
        $("input[name='totalBEDamount']").val(sum.toFixed(2));
    }

我正在使用上面的jQuery來獲取值的總和。 問題是它可以像固定布局固定布局小提琴一樣工作,但是當我從哈希中選擇單選按鈕創建文檔時, 帶有錯誤的代碼小提琴不起作用? 為什么???

使用[class='amount']並使用on() (用於動態創建的元素 )代替[class='column']例如,

$(document).ready(function () {
     $(document).on('keyup',"input[name*='bed'][class='amount']", function () {
         var sum = 0;
         //iterate through each textboxes and add the values
         $("input[name*='bed'][class='amount']").each(function () {
             //add only if the value is number
             if (!isNaN(this.value) && this.value.length != 0) {
                 sum += parseFloat(this.value);
             }

         });
         $("input[name='totalBEDamount']").val(sum.toFixed(2));
     });
});

工作演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM