2

You will see the sample code.

Below are my problems: -I have to select the services which has a value, but when I cancel the services the total price increase instead of decrease. -After I tested it, I cancel all the services, but the total price is still there. How can I get it back to zero?

Thank you! :)

Here's my sample code:

$(".chzn-select").chosen();
                    $("#cboservices").on("change", function(evt, params){
                        alert($(this).val());
                        //console.log($(this).val());
                        parray = $(this).val();
                        for (pcode in parray) {
                            get_priceList(parray[pcode]);
                            //console.log(sprice * totaltime);
                            tprice = tprice + (sprice * totaltime);
                        }

                        $("#subTotalPrice").html(number_format(tprice, 2, ".", ""));
                        console.log($("#subTotalPrice").html());            
                    });



                }
            },



Below are the sample HTML. I can't paste the whole code because it has limit. Thank you for the quick reply.


reservationDetails =
        '<div id="modal_div" class="widgets">'+
        '        <div class="widget widgetFix">'+
        '            <div class="title"><img src="images/icons/color/user-silhouette-question.png" alt="" class="titleIcon" /><h6>Information</h6></div>'+
        '            <br />'+
        '                <div id="refdiv" class="formRow modalFix">'+
        '                    <label>Reference No:</label>'+
        '                    <div class="formRight"><label class="labelFix inputFix" id="refNoValue">0</label></div>'+
        '                    <div class="clear"></div>'+
        '                </div>'+
        '                <div class="formRow modalFix">'+
        '                    <label>Room No:</label>'+
        '                    <div class="formRight"><label class="labelFix inputFix" id="roomNoValue">0</label></div>'+
        '                    <div class="clear"></div>'+
        '                </div>'+
        '                <div class="formRow modalFix">'+
        '                    <label>Name:</label>'+
        '                    <div class="formRight"><input type="text" id="customerNameValue" name="customerNameValue"/></div>'+
        '                    <div class="clear"></div>'+
        '                </div>'+

        '                <div class="formRow modalFix">'+
        '                <label>Services:</label>'+
        '                    <div class="formRight labelFix">'+
        '                        <select id="cboservices" data-placeholder="Choose your service(s)..." class="chzn-select" multiple="multiple" tabindex="6">'+
        '                       </select>'+  
        '                    </div>'+ 
        '                </div>'+ 

        '                <div class="formRow modalFix">'+
        '                    <label>Price:</label>'+
        '                    <div class="formRight"><label class="labelFix inputFix" id="subTotalPrice">0.00</label></div>'+
        '                    <div class="clear"></div>'+
        '                </div>'+

        '                <div class="clear"></div>'+ 

        '                <div class="formRow modalFix">'+
        '                <label>Personnel:</label>'+
        '                    <div class="formRight labelFix">'+
        '                        <select id="cbopersonnels" data-placeholder="Choose your attendant(s)..." class="chzn-select" multiple="multiple" tabindex="6">'+
        '                       </select>'+  
        '                    </div>'+ 
        '                </div>'+ 
        '                <div class="clear"></div>'+ 

        '                <div class="formRow modalFix">'+
        '                    <label>Date:</label>'+
        '                    <div class="formRight"><label class="labelFix inputFix" id="dateValue">0</label></div>'+
        '                    <div class="clear"></div>'+
        '                </div>'+
        '                <div class="formRow modalFix">'+
        '                     <label>Start Time:</label>'+
        '                     <div class="formRight"><label class="labelFix inputFix" id="startTimeValue">0</label></div>'+
        '                    <div class="clear"></div>'+
        '                </div>'+
        '                <div class="formRow modalFix">'+
        '                    <label>End Time:</label>'+
        '                    <div class="formRight"><label class="labelFix inputFix" id="endTimeValue">0</label></div>'+
        '                    <div class="clear"></div>'+
        '                </div>'+
        '                <div class="formRow modalFix">'+
        '                    <label>Status</label>'+
        '                    <div class="formRight ">'+
        '                        <select id="statusValue" name="statusValue">'+
        '                           <option value="reserved">Reserved</option>'+
        '                           <option value="paid">Paid</option>'+
        '                        </select>'+
        '                    </div>'+
        '                    <div class="clear"></div>'+
        '                </div>'+
        '                <div class="formRow modalFix" style="margin-top:3px">'+
        '                    <label>Remarks</label>'+
        '                    <div id="remarksdiv" class="formRight ">'+
        '                        <textarea id="remarksValue" name="remarksValue" style="resize:none;"></textarea>'+
        '                    </div>'+
        '                    <div class="clear"></div>'+
        '                </div>'+
        '            <br />'+
        '        </div>'+  
        '    <div class="clear"></div>'+
        '</div>';
4

2 回答 2

3

每次进入循环时,必须先将金额值设置为 0。

例如:

$("#cboservices").on("change", function(evt, params){
    alert($(this).val());
    //console.log($(this).val());
    parray = $(this).val();
    tprice = 0; //insert this line <<<<<<<<<<<<<<<<<=====[xxx]
    for (pcode in parray) {
        get_priceList(parray[pcode]);
        //console.log(sprice * totaltime);
        tprice = tprice + (sprice * totaltime);
    }

$("#subTotalPrice").html(number_format(tprice, 2, ".", ""));
console.log($("#subTotalPrice").html()); 


});

另外我想补充一点 jquery 选择的插件功能:

$("cboservices").on("change", function(){});

每次选择和/或取消选择标签<option></option>上的元素时都会调用。<select></select>

于 2013-10-24T04:52:04.273 回答
1

而不是你的

for(something in something){ ... }

本质上,你想要这样的东西:

// Define subtotal outside of loop
subtotal = 0

// This is like your for() loop
$('.price_element_selector').map(function(el){

  // get the individual price of each cart item
  price = parseFloat($(el).val())

  subtotal += price

});

formatMoney(subtotal);
于 2013-09-20T14:17:10.683 回答