微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 如何在Javascript中计算多个产品的总量?价值来自AJAX

$.ajax({
    type: "POST",
    url: 'ajax_subtotal.PHP',
    data: listing_id=listId,
    dataType:'json',
    success: function(data)
    {
        if(data['result']=='success')
        {
            alert(data['pricing']);
        }
}

data [‘pricing’]给我一个产品的价格.如果产品数量发生变化,该功能正在调用.那么,如何同时计算多个产品的总价?

解决方法:

First i store all the value in to the hidden field..

<input type="hidden" name="NumberOfProperty[]" id="NumberOfPropertyTxt<?PHP echo $id;?>" value=""/>

When my ajax is calling i got all the value in it..

var values=$('input[name="NumberOfProperty[]"]').map(function()
{
return this.value
}).get();
console.log(values);

In this i got my all price stored in to the array...then

var Total=0;
                        $('input[name="NumberOfProperty[]"]').each(function()
{
if($(this).val()>0)
{
Total+= parseFloat($(this).val());
}
});
$('#sub_total').val(Total);
$('#subtotal').html(Total);

I got all the value into the Total and assign it in to the sub_total id.    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐