jQuery - Building object arrays with html5 data attribute selection
I'm using HTML 5 data attributes to key rows of data in a table. I need to iterate the rows and gather the data into an object collection. I created a class to represent my row data:
function SomeItem(description, finalPrice, percentDi开发者_JS百科scount) {
this.Description = description;
this.FinalPrice = finalPrice;
this.PercentDiscount = percentDiscount;
}
An event fires which triggers the collection of this data.
$.each($('.ItemPriceBox'), function () {
var uniqueid = $(this).data('uniqueid');
var finalPrice = $(this).val();
});
The final piece of data, percentDiscount should be retrieved using the data-uniqueid
attribute. I'm not sure how to do this.
I want...
SomeItem[] items;
$.each($('.ItemPriceBox'), function () {
var uniqueid = $(this).data('uniqueid');
var finalPrice = $(this).val();
var percentDiscount = $('.ItemDiscountBox').where("uniqueid = " + uniqueid);
items[i] = new SomeItem(uniqueid,finalPrice,percentDiscount);
});
How can I solve this?
may be like this
var percentDiscount = $(".ItemDiscountBox[uniqueid='"+uniqueid +"']");
Instead of your pseudo-code where
, use filter:
var percentDiscount = $('.ItemDiscountBox').filter("[uniqueid='" + uniqueid + "']");
$('.ItemPriceBox').each( function (i) {
var uniqueid = $(this).data('uniqueid');
var finalPrice = $(this).val();
var percentDiscount = $('.ItemDiscountBox').filter("[uniqueid='" + uniqueid + "']");
items[i] = new SomeItem(uniqueid,finalPrice,percentDiscount);
});
You can also use .each()
instead of jQuery.each()
精彩评论