百分比计算和自动逗号组合

时间:2014-03-07 18:02:33

标签: jquery ajax

我有问题。当我将它组合成自动逗号功能时,我的百分比计算有问题。如果我插入一个数字,例如100,000,百分比是10%,我的百分比计算是我输入的前100个数据。所以计算是110只我不怎么使用那个功能的组合。请帮忙。提前谢谢。

百分比计算代码:

/* attach a submit handler to the form */
    $(document).ready(function() { 
        $('#submitme').on('submit', function(e) {
            e.preventDefault();
            var mytxt1 = [];
            var mytxt2 = [];
            var mytxt3 = [];
                $(".expense_name").each(function () {
                    mytxt1.push($(this).val());
                });             
                $(".expense_desc").each(function () {
                    mytxt2.push($(this).val());
                });     
                $(".expense_cost").each(function () {
                    mytxt3.push($(this).val());
                });     

                var perfTimes = $(this).serialize();
                $.post("addfunction.php", {results: perfTimes, txt1: mytxt1,  txt2: mytxt2,  txt3: mytxt3 }, function (data) {

if (data.errors) {
    var alertErrors = "The following errors were found: ";
    $.each(data.errors, function(index, error){
       alertErrors += "\n" + "\n" + error.message;//Add each error in a new line 

    });
    alert(alertErrors);
} 
else {
    alert(data.message);
    window.location.href = data.redirect;
            }
       }, "json");
            });
        });
</script>
<script>
    var nitem =0; 
    var ntotal = 0;

        function totalItemExpence(){
            ntotal = 0;
                $('.expense_cost').each(function(){ 
                    if($(this).val() != ""){
                        ntotal += parseFloat($(this).val()); 
                        }
                    }); 
                //$('#total').val(ntotal); 
            }
        $(document).on('change keyup paste', '.expense_cost', function() { 
            totalItemExpence();
            mytotal();
            }); 

        $('.btn').click(function() { 
            nitem++; 
                $('#wrapper').append('<div id="div' + nitem + '" class="inputwrap">' +
                    '<input class="expense_name" placeholder="Expense Name" id="' + nitem + '" required/>' +
                    '<input class="expense_desc" placeholder="Expense Description" id="' + nitem + '" required/>' +
                    '<input class="expense_cost" placeholder="Expense Cost" id="' + nitem + '" required/> ' +
                    '<br><br></div>');  
                }); 

        $('.btn2').click(function() {           
            ntotal = $('#total').val(); 
                $("#div" + nitem + " .expense_cost").each(function(){               
                    if($(this).val() != ""){
                        ntotal -= parseFloat($(this).val()); 
                        }
                    }); 

                    $("#div" + nitem ).remove();
                        nitem--; 
                    $('#total').val(ntotal); }); 




        var textbox = $("#txtTaxPercent");
        var ResultTextbox = $("#txtfctTaxValue");
        var PriceTextbox = $("#txtPurePrice");
        var pricetotal = $("#total");
        var pasamame = $(".expense_cost");

            $([PriceTextbox[0]]).bind("change keyup paste", function(e) {
                totalItemExpence();
                mytotal();
            });  

            $([textbox[0]]).bind("change keyup paste", function(e) {
                mytotal();  
            });  

            function mytotal()
            {
                $("#txtfctTaxValue").val(0);
                var Result;
                if(PriceTextbox.val()!=="" ){
                    Result1 = (parseInt(PriceTextbox.val())) * parseFloat(textbox.val() / 100);
                    Result = (parseInt(PriceTextbox.val())) * parseFloat(textbox.val() / 100) + (parseInt(PriceTextbox.val()));

                }

                else{
                    Result = 0;
                }
                Result = parseFloat( Result) + ntotal;
                pricetotal.val(Result);
                ResultTextbox.val(Result1); 

            }

自动逗号代码:

       $(document).ready(function(){
         $('input.number').keyup(function(event){
             // skip for arrow keys
             if(event.which >= 37 && event.which <= 40){
                 event.preventDefault();
             }
             var $this = $(this);
             var num = $this.val().replace(/,/gi, "").split("").reverse().join("");

             var num2 = RemoveRougeChar(num.replace(/(.{3})/g,"$1,").split("").reverse().join(""));

             console.log(num2);


             // the following line has been simplified. Revision history contains original.
             $this.val(num2);
         });
       });

       function RemoveRougeChar(convertString){


           if(convertString.substring(0,1) == ","){

               return convertString.substring(1, convertString.length)            

           }
           return convertString;

       }

1 个答案:

答案 0 :(得分:0)

parseInt()尽力创建一个整数。如果您有一个非数字值(逗号),它将停在那里。您需要在调用parseInt()

之前删除逗号

执行类似这样的操作,使用字符串上的替换来消除不需要的字符。

if(PriceTextbox.val()!=="" ){
    var price = parseInt(PriceTextbox.val().replace(',','').replace('.',''));
    Result1 =price * parseFloat(price / 100);
    Result = price * parseFloat(price / 100) + (price);

}

如果您只想要美元部分,请删除第二个替换调用(即删除小数点)。那样10,000.00将是10000而不是1000000。

  var price = parseInt(PriceTextbox.val().replace(',',''));

http://jsfiddle.net/CvnhV/