Question

The code is getting a price value from a hidden input field called '#orginalAddon', the cost of any additional addons is then added to this value and the result displayed to the user. The problem is that the calculated total output in '#addonPrice' appears to be truncated if the '#orginalAddon' value is over 1,000.

The code is as follows:

$( ".add_div" ).click(function(){
                $('#addons_loader').html('<div id="resloading"><img src="<?=BASE_URL?>/img/ajax-loader.gif" /></div>');

                var id = $( this ).attr('rel');
                $('#AddOnAddForm input#AddOnId').attr('value',id);

                /* load addon into addons li (session) */
                $.getJSON('<?=BASE_URL?>/res/addons_add/'+id, function(data) {

                    $('#addons_ajax').html('');
                    $('#addons_loader').html('');

                    var items = [];
                    var totalprice = 0;

                    // Generate elements for right side grey box listing addons 
                    $.each(data, function(key, obj) {
                        items.push('<li id="' + obj.id + '">' + obj.title + '€' + obj.price + '</li>');
                        totalprice += parseFloat(obj.price);
                    });

                    $('<ul/>', {
                        'id': 'addons_list',
                        html: items.join('')
                    }).appendTo('#addons_ajax');

                    $("#addonPrice").html(parseFloat($("#orginalAddon").val()) + totalprice);

                });

Any advice appreciated...

Était-ce utile?

La solution

If you put comma to separate thousands, remove it

e.g:

parseFloat(obj.price.replace(/,/g,''));

edit:

for your input:

parseFloat($("#orginalAddon").val().replace(/,/g,''))
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top