简体   繁体   中英

JQuery calculate values for dynamically added fields

I'm trying to generate a price based on radio button selections, on input fields that are added (sort of like a list). Unfortunately since each set of radio buttons added has a unique name (so that they can be parsed separately later) the function that calculates the price is pulling the radio buttons value by it's name. This is clearly problematic since there are multiple instances of the same scenario.

Anyone? http://jsfiddle.net/pxrpF/1/

I'm also looking to generate a Grand Total that will add up each set, so if anyone can help me figure these two bits out that would be wonderful!

This should handle the problem with grabbing the right radio buttons:

var containerCount = $(this).parents('.container').prevAll('.container').size();

var $r = $('.container:eq('+containerCount+')').find(cardType);
var $d = $('.container:eq('+containerCount+')').find(cardQ);

EDIT (to incorporate conversation below)

In addition, the selector strings cardType and cardQ should be as follows:

var cardType = ":radio[name*='type']:checked"
var cardQ = ":radio[name*='quantity']:checked"

The *= ensures that the attribute starts with type or quantity respectively.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM