SPServices Sum返回值

时间:2016-12-07 18:15:39

标签: javascript sharepoint spservices

我正在使用SPServices查询SharePoint 2010列表,只能获取我想要的值,但是当我尝试将其全部加起来时,它只显示“作为字符串”

alert of sum calculation

以下是我使用的代码:

var TotalAmount = 0.00;

$().SPServices({
    operation: "GetListItems",
    async: false,
    listName: {list guid},
    viewName: {view guid},
    CAMLViewFields: "<ViewFields><FieldRef Name='Title' /><FieldRef Name='AP' /><FieldRef Name='Amount' /></ViewFields>",
    CAMLQuery: "<Query><Where><Eq><FieldRef Name='AP' /><Value Type='Text'>"+areaValue+"</Value></Eq></Where></Query>",
        completefunc: function (xData, Status){
            $(xData.responseXML).SPFilterNode("z:row").each(function(){
                var liHtml=$(this).attr("ows_Title");
                var app =$(this).attr("ows_AP");
                Amount = $(this).attr("ows_Amount");


                TotalAmount = TotalAmount + montante;


            });     
        }
});
  alert(TotalAmount);

感谢任何帮助

1 个答案:

答案 0 :(得分:0)

您可以使用Number()函数将字符串值转换为JavaScript中的数字。

Amount = $(this).attr("ows_Amount");
TotalAmount = TotalAmount + Number(Amount);

请注意,您还可以使用通用简写将变量括在括号中,并在其前面加上加号(+),该加号指示JavaScript尝试将变量解析为数字。

Amount = +( $(this).attr("ows_Amount") );
TotalAmount = TotalAmount + Amount;