2016-10-06 5 views
1

Ich habe eine Tabelle mit einer Texteingabe in jeder Zeile. Benutzer können einen Dollarbetrag in jedem Textfeld angeben. Mein Code durchläuft jede Texteingabe und fasst die Werte zusammen. Mein Problem ist, dass wenn ein Benutzer einen Wert über> 1.000.000 eingibt, die Summe falsch wird. Zum Beispiel, wenn ein Benutzer 1.000.000 eingibt, ist die Summe 1.000.Falsche Platzierung von Komma in Summe

function init_icheck() { 
    $('#datatable input[type=checkbox]').iCheck({ 
     checkboxClass: 'icheckbox_square-blue', 
     increaseArea: '10%' 
    }); 
} 

// When Pay in Full Checkbox is Checked fill in Pay This Time Field with Invoice Amount Due Value 
function paynow() { 
    var payFull = $('input[type="checkbox"].payfull'); 
    payFull.on('ifChecked', function(event) { 
     $(this).parents('tr').find('.paynow').val($(this).val().replace('$', '')); 
     CalcFooter(); 
    }); 
} 

// If Pay in Full Unchecked then remove value from respective Pay This Time Input 
// Only bind the ifUnchecked event if the checkbox is checked 
function remove_checkbox() { 
    var payFull = $('input[type="checkbox"].payfull'); 
    payFull.on('ifUnchecked', function(event) { 
     if ($(this).parents('tr').find('.paynow').val() == $(this).val().replace('$', '')) { 
      $(this).parents('tr').find('.paynow').val(''); 
      CalcFooter(); 
     } 
    }); 
} 

// If Pay This Time changes recalculate total 
function recalc_total() { 
    $('.paynow').keyup(function() { 
     var $ThisCheck = $(this).parents('tr').find('.payfull'); 
     // Add Commas if # is over 1,000 
     $(this).val(addCommas($(this).val().replace(/,/g, ''))); 
     if ($(this).val() == $ThisCheck.val().replace('$', '')) { 
      $ThisCheck.iCheck('check'); 
     } else { 
      $ThisCheck.iCheck('uncheck'); 
     } 
     CalcFooter(); 
    }); 
} 


// Recalc Function 
function CalcFooter() { 
    var amtPage = 0; 
    var amtTotal = 0; 

    var Sum = 0; 
    $('.paynow').each(function(index, Obj) { 
     var value = parseFloat($(this).val().replace(',', '')); 
     if (!isNaN(value)) amtPage += value; 
    }); 

    $('#datatable').DataTable().$('.paynow').each(function(index, Obj) { 
     var value = parseFloat($(this).val().replace(',', '')); 
     if (!isNaN(value)) amtTotal += value; 
    }); 

    $('#amounttopay').text(
     'Page: $' + addCommas(amtPage.toFixed(2)) + 
     '/Total: $' + addCommas(amtTotal.toFixed(2)) 
    ); 
} 

// Add Commas if value > 1,000 
addCommas = function(input) { 
    // If the regex doesn't match, `replace` returns the string unmodified 
    return (input.toString()).replace(
     // Each parentheses group (or 'capture') in this regex becomes an argument 
     // to the function; in this case, every argument after 'match' 
     /^([-+]?)(0?)(\d+)(.?)(\d+)$/g, 
     function(match, sign, zeros, before, decimal, after) { 
      // Less obtrusive than adding 'reverse' method on all strings 
      var reverseString = function(string) { 
       return string.split('').reverse().join(''); 
      }; 
      // Insert commas every three characters from the right 
      var insertCommas = function(string) { 
       // Reverse, because it's easier to do things from the left 
       var reversed = reverseString(string); 
       // Add commas every three characters 
       var reversedWithCommas = reversed.match(/.{1,3}/g).join(','); 
       // Reverse again (back to normal) 
       return reverseString(reversedWithCommas); 
      }; 
      // If there was no decimal, the last capture grabs the final digit, so 
      // we have to put it back together with the 'before' substring 
      return sign + (decimal ? insertCommas(before) + decimal + after : insertCommas(before + after)); 
     } 
    ); 
}; 

// Reinitialize iCheck on Pagination Change 
$('#datatable').on('draw.dt', function() { 
    init_icheck(); 
    paynow(); 
    recalc_total(); 
    remove_checkbox(); 
    CalcFooter(); 
}); 

// Initialize Datatables 
$('#datatable').dataTable({ 
    "stateSave": true, 
    "oLanguage": { 
     "sSearch": "Search Results:" 
    } 
}); 

Ich habe eine einfache jsfiddle, die dieses Problem veranschaulicht. Ich danke Ihnen im Voraus dafür, dass Sie mich in die richtige Richtung weisen.

http://jsfiddle.net/tgf59ezr/14/

Antwort

3

Mit .replace(',', '') ersetzt nur der ersten Instanz der gesuchten Zeichenfolge, wodurch die Anzahl Parsen wie beabsichtigt nicht funktionieren.

Verwenden Sie stattdessen:

.replace(/,/g, '') 

Die g Mittel, um alle Instanzen ersetzen.

http://jsfiddle.net/da03j0aa/

Referenz: String.prototype.replace()

+0

Ja, das war es. Guter Fang! Vielen Dank. – dentalhero