javascript - Checking multiple prices, increase total price label -
i'm attempting add total price on website whenever user clicks on checkbox - using jquery, recieving $nan when add results. alert box near end of script showing weird results (leading 0s, i'm not sure why)
here's current code:
$("#additionaloffers").bind("click", "input:checkbox", function () { var $items = $("#additionaloffers").find("input:checkbox:checked"); var $total = $("#totalprice"); var cur_total = 0; $items.each(function () { var $this = $(this); var target = $("span[class='" + $this.attr("id") + "']"); var item_value = +target.text() cur_total += item_value; alert(cur_total); }); $total.html("$" + cur_total); });
because i'm using .net, markup looks this:
<asp:checkbox id="checkbox7" cssclass="label-for-check" text="lease/sales consultation" runat="server" /> <span class="checkbox7">75</span> <br /> <asp:checkbox id="checkbox1" cssclass="label-for-check" text="lease review advisor" runat="server" /> <span class="checkbox1">50</span> <br /> <asp:checkbox id="checkbox2" cssclass="label-for-check" text="starter package" runat="server" /> <span class="checkbox2">25</span> <br />
i feel there silly reason isn't working. appreciated!
the reason you're using asp.net, ids dynamically generated , $this.attr("id")
returns different value span class in
var target = $("span[class='" + $this.attr("id") + "']");
for example, when span class checkbox7
, $this.attr("id")
returns {prefix}checkbox7
.
solution 1: replace line with:
var target = $this.next();
solution 2:
in case need find class, use attribute other id. in code-behind:
checkbox7.attributes["myspanclass"] = "checkbox7"; checkbox1.attributes["myspanclass"] = "checkbox1"; checkbox2.attributes["myspanclass"] = "checkbox2";
and replace line with:
var target = $("span[class='" + $this.attr("myspanclass") + "']");
Comments
Post a Comment