简体   繁体   English

jQuery-使用html5数据属性选择构建对象数组

[英]jQuery - Building object arrays with html5 data attribute selection

I'm using HTML 5 data attributes to key rows of data in a table. 我正在使用HTML 5数据属性来对表中的数据行进行键控。 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, percentDiscount) {
    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. 最后的数据percentDiscount应该使用data-uniqueid属性进行检索。 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? 我怎么解决这个问题?

可能是这样

var percentDiscount = $(".ItemDiscountBox[uniqueid='"+uniqueid +"']");

Instead of your pseudo-code where , use filter: 代替您的伪代码where ,使用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() 您也可以使用.each()代替jQuery.each()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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