开发者

Jquery selectors in a loop give unclear results

开发者 https://www.devze.com 2023-02-02 04:49 出处:网络
I am confused: whats going on with the following code var ProductFeatures = []; for (var i = 1; i < 3; i++) {

I am confused: whats going on with the following code

var ProductFeatures = [];

for (var i = 1; i < 3; i++) {
    Pr开发者_StackOverflow中文版oductFeatures.push({
        Guid: $('#FeatureListTable tr').eq(i).attr('id'),
        Value: $('#FeatureListTable td:nth-child(5)').eq(i-1).val(),
        Remark: $('#FeatureListTable td:nth-child(6) input').eq(i-1).val()
    });
}

When I comment out the "Value:" row, I get a different result in the Remark field than when there are no comments

// Value: $('#FeatureListTable td:nth-child(5)').eq(i-1).val(),

Why does this happen?

Thanks in advance, Julian


Not sure why the Remark value would change when you comment out the Value line, but this line:

Value: $('#FeatureListTable td:nth-child(5)').eq(i-1).val(),

...would only return null or undefined (I don't remember which one), because it is trying to get the value of a <td> instead of a form input.

This sort of data structure is much better built using jQuery's .map() method.

0

精彩评论

暂无评论...
验证码 换一张
取 消