js 动态计算折扣后总价格

 <script type="text/javascript">
      
<!---计算折扣后的总价格--->
function outtotalprice(i) { document.getElementsByName("items[#index#].TotaPricel")[i].value = document.getElementsByName("items[#index#].qty")[i].value * document.getElementsByName("items[#index#].Price")[i].value; } <!----在动态显示 原价、折扣、折扣后的总价---->
function OutTotalPrice() {
var len = document.getElementsByName("items[#index#].TotaPricel").length; var ordertotalprice = 0; for (var j = 0; j < len; j++) { ordertotalprice += parseFloat(document.getElementsByName("items[#index#].TotaPricel")[j].value); } document.getElementById("Out.TotalPrice").value = ordertotalprice.toFixed(2); var strdiscount = parseFloat(parseInt(document.getElementById("Out.discount").value) / 100).toFixed(2); var strdiscountPrice = parseFloat(ordertotalprice * strdiscount).toFixed(2); document.getElementById("Out.discountPrice").value = strdiscountPrice; document.getElementById("spanoldprice").innerHTML = ordertotalprice.toFixed(2); document.getElementById("spandistinct").innerHTML = document.getElementById("Out.discount").value; document.getElementById("spandisprice").innerHTML = strdiscountPrice; } </script>

 

声明两个对象:


<%: Html.HiddenFor(model => model.TotalPrice, new { id = "Out.TotalPrice" })%> <%: Html.HiddenFor(model => model.discountPrice, new { id = "Out.discountPrice" })%>

 

   <div class="sortDrag" style="width: 20%; border: 1px solid #e66; margin: 5px; float: inherit;min-height: 220px;">
    <div style="border: 1px solid #B8D0D6; padding: 10px; margin: 10px">
     原价(¥)&nbsp;&nbsp;&nbsp;<span id="spanoldprice" style="color: Red; font-size: x-large"><%=ViewData["totalPrice"]%></span></div>
      <div style="border: 1px solid #B8D0D6; padding: 10px; margin: 10px">
     折扣(%)&nbsp;&nbsp;&nbsp;<span id="spandistinct" style="color: Red; font-size: x-large"><%=ViewData["discount"]%></span></div>
    <div style="border: 1px solid #B8D0D6; padding: 10px; margin: 10px">
     总价(¥)&nbsp;&nbsp;&nbsp;<span id="spandisprice" style="color: Red; font-size: x-large"><%=ViewData["discountPrice"]%></span></div>
     </div>

后台代码

 private string BindTableOutSend(IList<VOutProceManager> objList)
        {
            sbProcessingtype = null;
            getProcessingtype();
            string strCalMethod = "";
            StringBuilder sb = new StringBuilder();
            int i = 0;
            string currentdate = DateTime.Now.ToString("yyyy-MM-dd");
            foreach (VOutProceManager obj in objList)
            {
                if (ViewBag.outauditstate <= 0)
                {
                    sb.Append("<tr class=\"unitBox\">");
                    sb.Append("<input type=\"hidden\"   name=\"items[#index#].Id\"   submitName=\"items[" + i + "].Id\" value=\"" + obj.Id + "\" /> ");
                    sb.Append("<input type=\"hidden\"   name=\"items[#index#].bomID\"   submitName=\"items[" + i + "].bomID\" value=\"" + obj.bomID + "\" /> ");
                    sb.Append("<input type=\"hidden\"   name=\"items[#index#].Isbom\"   submitName=\"items[" + i + "].Isbom\" value=\"" + obj.Isbom + "\" /> ");
                    sb.Append("<td  ><input class=\"textInput \"  size='15'  readonly=\"readonly\"  value=\"" + obj.mouldNo + "\"  > </td>");
                    sb.Append("<td  ><input class=\"textInput \"  size='9'   readonly=\"readonly\"  value=\"" + obj.partName + "\"  > </td>");
                    sb.Append("<td  ><input class=\"textInput \"  size='3'   readonly=\"readonly\"    value=\"" + obj.drawingNo + "\"  > </td>");
                    sb.Append("<td  ><input class=\"textInput \"  size='9'   readonly=\"readonly\"   name=\"items[#index#].qty\"     submitName=\"items[" + i + "].qty\"  value=\"" + obj.qty + "\"  > </td>");
                    sb.Append("<td  ><input class=\"textInput \"  size='15'  readonly=\"readonly\"   name=\"items[#index#].typeName\"  submitName=\"items[" + i + "].typeName\" value=\"" + obj.typeName + "\"  > </td> ");
                    sb.Append("<td>");
                    sb.Append("<SELECT  name=\"items[#index#].PriceType\"  submitName=\"items[" + i + "].PriceType\">");
                    strCalMethod = obj.PriceType;
                    if (strCalMethod == "体积")
                    {
                        sb.Append("<OPTION selected value='体积'>体积</OPTION><OPTION value=面积>面积</OPTION>");
                    }
                    else
                    {
                        sb.Append("<OPTION value='体积'>体积</OPTION><OPTION selected value=面积>面积</OPTION>");
                    }
                    sb.Append("</SELECT>");
                    sb.Append("</td>");
                    sb.Append("<td  ><input class=\"textInput required number\" size='9'  name=\"items[#index#].Quantity\"     submitName=\"items[" + i + "].Quantity\" value=\"" + obj.Quantity + "\"  > </td>");
                    sb.Append("<td  ><input class=\"textInput\" type=\"lookup\" size='3' name=\"items[#index#].Unit.Name\" lookupgroup=\"items[#index#].Unit\" suggesturl=\"projectGL/Mould/GetName?typeid=7\"  suggestfields=\"Name\" postfield=\"keywords\" submitName=\"items[" + i + "].Unit.Unit\" value=\"" + obj.Unit + "\"  > </td>");
                    sb.Append("<td  ><input class=\"textInput required number\" size='9'  name=\"items[#index#].Price\" onkeyup=\"outtotalprice(" + i + ");OutTotalPrice()\"   submitName=\"items[" + i + "].Price\" value=\"" + obj.Price + "\"  > </td>");
                    sb.Append("<td  ><input class=\"textInput \" size='9'  readonly=\"readonly\"   name=\"items[#index#].TotaPricel\"   submitName=\"items[" + i + "].TotaPricel\" value=\"" + obj.TotaPricel + "\"  > </td>");
                    sb.Append("</tr>");
                    i++;
                }
                            }
            return sb.ToString();
        }

首次加载:初始化的后台显示(未输入价格前)

   ViewData["totalPrice"] = 0;
   ViewData["discount"] = "100%";
   ViewData["discountPrice"] = 0;

在提交的方法里

判断是否输入单价:把每种类型的总价累加起来,得到最后的总价:

decimal totalprice = 0.0m;
decimal countprice = 0.0m;
bool iszero = true;
while (!string.IsNullOrEmpty(Request.Form["items[" + i + "].Id"]))
    {  totalprice = decimal.Parse(Request.Form["items[" + i + "].TotaPricel"]);
      countprice += totalprice;
      ViewData["totalPrice"] = countprice;
      if (totalprice <= 0)
        {
         iszero = false;
         break;
         }
      i++;
    }
  if (!iszero)
  {
     return SaveSuccessStr("300", "单价不能小于等于0!", "", "", "");
  }

 

在提交的方法里计算折扣后的总价格:

  if (Tmodels.discount == 100)
       Tmodels.discountPrice = countprice;
  else
       Tmodels.discountPrice = Convert.ToDecimal(countprice * (Tmodels.discount / 100.0m));

 

 

 

posted on 2013-11-13 14:14  守望星空  阅读(2637)  评论(0编辑  收藏  举报

导航