循环通过每个但功能未知

时间:2016-03-14 13:56:49

标签: jquery

我得到了json值,我在rentFeatureOption上循环。这部分工作。

{
    "bailId": 2,
    "fromDate": "2016-03-11",
    "toDate": null,
    "lodgerId": 1,
    "supportTwoPerson": false,
    "lodgerPayforTwo": false,    
    "roomId": 1,
    "appartmentId": 1,
    "price": 500.0,
    "paymentPeriod": "WEEK",
    "address": "460 Ch. Chambly, Longueuil, 1",
    "rentFeatureOption": [{
        "rentFeatureOptionId": 1,
        "featureOptionId": 2,
        "price": 5.0
    }]
}

我需要在html表上循环

<tbody>
    <tr data-index="0" data-rent-feature-option-id="">
        <td style="" data-feature-option-id="1">Air climatisé
            <input type="hidden" name="rentFeatureOption[0].rentFeatureOptionId" value="">
            <input type="hidden" name="rentFeatureOption[0].featureOptionId" value="1">
        </td>
        <td style="">
            <input type="text" name="rentFeatureOption[0].price" value="">
        </td>
    </tr>
    <tr data-index="1" data-rent-feature-option-id="">
        <td style="" data-feature-option-id="2">Internet
            <input type="hidden" name="rentFeatureOption[1].rentFeatureOptionId" value="">
            <input type="hidden" name="rentFeatureOption[1].featureOptionId" value="2">
        </td>
        <td style="">
            <input type="text" name="rentFeatureOption[1].price" value="">                
        </td>
    </tr>
</tbody>
for (var i = 0; i < data.rentFeatureOption.length; i++) {
    $('#lodgerRentOptionTableResult > tbody  > tr').each(function() {            
        if ($(this).find('td')[0].data("feature-option-id") == data.rentFeatureOption[i].featureOptionId) { 
        }
    });                        
}

条件我收到此错误

  

未捕获TypeError:$(...)。find(...)[0] .data不是函数

1 个答案:

答案 0 :(得分:2)

[0]返回一个dom元素。您可以改为使用.first()eq(0)

您也可以使用一次选择器而不是两个选择器:

$('#lodgerRentOptionTableResult > tbody  > tr > td:first-of-type').each(function() {            
    if( $(this).data("feature-option-id")==data.rentFeatureOption[i].featureOptionId){