jQuery changing fields to substring of related field
Posted
by Katherine
on Stack Overflow
See other posts from Stack Overflow
or by Katherine
Published on 2010-04-01T00:39:18Z
Indexed on
2010/04/01
0:43 UTC
Read the original article
Hit count: 508
Another jquery calculation question.
I've this, which is sample code from the plugin site that I am playing with to get this working:
function recalc(){
$("[id^=total_item]").calc(
"qty * price",
{
qty: $("input[name^=qty_item_]"),
price: $("input[name^=price_item_]"),
},
function (s){ return s.toFixed(2);},
function ($this){ var sum = $this.sum();
$("#grandTotal").val(
// round the results to 2 digits
sum.toFixed(2)
);
}
);
}
Changes in the price fields cause the totals to update:
$("input[name^=price_item_]").bind("keyup", recalc);
recalc();
Problem is I won't know the value of the price fields, they will be available to me only as a substring of values entered by the user, call it 'itemcode'. There will be a variable number of items, based on a php query.
I've come up with this to change the price based on the itemcode:
$("input[name^='itemcode_item_1']").keyup(function () {
var codeprice = this.value.substring(2,6);
$("input[name^='price_item_1']").val(codeprice);
});
$("input[name^='itemcode_item_2']").keyup(function () {
var codeprice = this.value.substring(2,6);
$("input[name^='price_item_2']").val(codeprice);
});
However while it does that, it also stops the item_total from updating. Also, I feel there must be a way to not need to write a numbered function for each item on the list. However when I just use
$("input[name^='itemcode_item_']")
updating any itemcode field updates all price fields, which is not good.
Can anyone point me in the right direction? I know I am a bit clueless here, but javascript of any kind is not my thing.
© Stack Overflow or respective owner