I'm relatively a newbie to jQuery. I am trying to create a timesheet
application. One of the things I am attempting to do is have the
totals column for each task.
I have the several textboxes laid out beside the text name (one
textbox per day, so 7 textboxes for the individual days + 1 for the
total column). The textboxes are named using the following convention:
ta_[taskid]_[calendar date]. The totals row is named tatoal_[taskid].
So I have something like this:
<input type="text" name="ta_1_20081117" value="0" id="ta_1_20081117" /
>
<input type="text" name="ta_1_20081118" value="0" id="ta_1_20081118" /
>
<input type="text" name="ta_1_20081119" value="0" id="ta_1_20081119" /
>
<input type="text" name="ta_1_20081120" value="0" id="ta_1_20081120" /
>
<input type="text" name="ta_1_20081121" value="0" id="ta_1_20081121" /
>
<input type="text" name="ta_1_20081122" value="0" id="ta_1_20081122" /
>
<input type="text" name="ta_1_20081123" value="0" id="ta_1_20081123" /
>
<input type="text" name="tatotal_1" style="width:25px;" value=""
id="tatotal_1" />
Right now, I am able to dynamically set a function (updateTotals) to
fire whenever one of the "day" textboxes are changed. The callback
function looks like this:
function updateTotal() {
total = 0;
$("[EMAIL PROTECTED]'ta_" + "1" + "']").each(function(i,v){
if(!isNaN(parseInt(v.value))) {
total += parseInt(v.value);
};
});
$("[EMAIL PROTECTED]'tatotal_" + "1" + "']").val(total);
}
The event is bound to the textboxes using the following code
$("input[name^='ta_1']").change(updateTotal);
I will have several rows of textboxes (e.g. ta_2, ta_3, ... ). Is
there a way for me to pass a parameter to the updateTotal callback so
have it do the totals for all of the tasks in the form?
Any other more efficient algorithms with greatly be appreciated.
Jett