tags:

views:

159

answers:

1

There is a one section of orders page where users can add new fields to add new products. They choose a product from a drop down menu and its price is written in another column. I need to add a functionality that will allow users to enter quantity amount, and total price will be updated as soon as quantity amount changes. I tried it with the following codes but I keep receiving 0 for the total price.

My codes.

$('#AddProduct').click(function() {
    var i = 0;
    var adding = $(+(i++)+'<div class="row'+(i)+'"><div class="column width50"><input type="text" id="PRODUCTNAME" name="PRODUCTNAME'+(i)+'" value="" class="width98" /><input type="hidden" class="PRODUCTID" name="PRODUCTID" /><input type="hidden" class="UNITPRICE" name="UNITPRICE'+(i)+'" /><small>Search Products</small></div><div class="column width20"><input type="text" class="UNITQUANTITY" name="UNITQUANTITY'+(i)+'" value="" class="width98" /><small>Quantity</small></div><div class="column width30"><span class="prices">Unit Price:<span class="UNITPRICE"></span><br />Total Price:<span class="TOTALPRICE"></span><br /><a href="#" id="RemoveProduct(".row'+(i)+'");">Remove</a></span></div>');    
    $('#OrderProducts').append(adding);

    adding.find("#PRODUCTNAME").autocomplete("orders.cs.asp?Process=ListProducts", {
        selectFirst: false
    }).result(function(event, data, formatted) {
        if (data) {
            adding.find(".UNITPRICE").html(data[1]);
            adding.find(".PRODUCTID").val(data[2]);
            adding.find(".TOTALPRICE").html(data[1] * $('.UNITQUANTITY').val()); 
        }
    });

    return false;
});


            <div id="OrderProducts">
                <a href="#" id="AddProduct"><img src="icons/add.png" alt="Add" /></a>
            </div>  
A: 

You function seems to act on the result of the auto complete so since the .UNITQUANTITY may not have been set it will not get anything other then 0 unless you put a value in for the UNITQUANTITY before you select from the auto complete. But you would need to leave that function as it but possibly add a default of 1 to the UNITQUANTITY then add another function handling the event that the UNITQUANTITY value has changed then do the last line of that inline function and you should have the results you want.

I would suggest adding the following right before your return false;

adding.find(".UNITQUANTITY").change(function()
  {
    adding.find(".TOTALPRICE").html(adding.find(".UNITPRICE").val() * adding.find(".UNITQUANTITY").val()); 
  });

Also edit your inline function to do the following...

adding.find(".UNITPRICE").html(data[1]);
adding.find(".UNITPRICE").val(data[1]);
adding.find(".PRODUCTID").val(data[2]);
adding.find(".TOTALPRICE").html(data[1] * $('.UNITQUANTITY').val()); 

Your first line was setting the html of the unit price but not the val so when we tried to retrieve with the .val() it returned 0

Also as a note you should consider adding some round to 2 decimal places to make sure you don't end up with too many decimals for a total price.

Unit Price:50.99

Total Price:2804.4500000000003

Jeff Beck
Jeff, adding 1 did solve my problem. I wonder how I would not see that for hours. But I still have no clue on adding that other function so that field will update everytime quantity is changed.
Efe Tuncel
I just edited my answer with a hint. Um I'm a bit sleepy so it may not be exactly correct. But it should point you in the correct direction.
Jeff Beck
well this seems correct to me. I tried it but it didnt work. When I update the quantity field, I get 0 for the total price again.
Efe Tuncel
Make sure that the adding.find(".UNITPRICE").val() is coming back correctly and the adding.find(".UNITQUANTITY").val() just alert them both and you should see if one is ending up wrong or not set correctly which would cause your problem.
Jeff Beck
Actually I'm betting that you need to set the unit price differently.
Jeff Beck
Do you think autocomplete is causing the problem?
Efe Tuncel
I found the problem I am editing now...
Jeff Beck
But the page does not give any errors. How could you find the error? Did you use firebug?
Efe Tuncel
Yep firebug and I saw the that hidden form field looked like <input type=hidden name=unitprice>7.55</input> when it should have a value attribute. I hope that solved your problem?
Jeff Beck
Efe Tuncel
In all honesty we could make this work if you had two different IDs one for the hidden field and one for the displayed price. I don't normally do the find so much with jQuery
Jeff Beck
Ok now, when I added adding.find(".UNITPRICE").val(data[1]); . I see correct updated values in both hidden and displayed fields. I am not sure this an efficient way of coding. My problem is solved but I would appreciate if you would look at it one last time and tell me it works and its neat. :)
Efe Tuncel
looks like you got it working
Jeff Beck
You should start using IDs so you can deal directly with just the hidden field or the display span. But it does seem nice
Jeff Beck
Thank you so much!
Efe Tuncel
Also, you are right. I will work with IDs next time I do something similar...
Efe Tuncel