views:

202

answers:

1

I have a listview with lists lines of an invoice (Which are editable), each invoice line has a qty and a rate and I want to show the line total on the client show so if the qty or rate is changed the total changes before the postback.

I have applied the below which gives the right result but doesn't work with multiple line items. The qty and rate fields have a class name of decimal and when they leave focus it will update the line total as per below but I need to restrict it to only update the total of the current line which it doesn't but inputs the total value of all lines with the sum of the first entry.

$(".decimal").blur(function() {           
       $(".total").val($(".qty").val() * $(".rate").val());

    });
+1  A: 

It should be possible to do this, but we'd need to see how your HTML is set up to recommend specific code.

Basically, in the function called when the blur event is fired, you have access to $(this), which will represent the particular field on which the blur event occurred. You should be able to use jQuery selectors to set the total for the particular field that has the .total class and is in the same line as $(this).

An example: if your HTML looked like this:

<div>
     <input type="text" class="decimal qty"> <input type="text" class="decimal rate"> <input type="text" class="total">
<div>

(repeated, of course, for the various rows)

Then the target total field will always be a sibling of the triggering decimal field, so, for example, you could do

$(this).parent().filter(".total").val(//and set your values here)
JacobM
+1 - you beat me by minutes and said basically what I was going to say :-) One thing I would add is that the closest function (http://docs.jquery.com/Traversing/closest) can help with the finding of the parent item. Using that one over parent() lets you "refactor" the html and not break the jQuery as quickly.
Dan F
And +1 to your comment -- very good point about "closest".
JacobM