Trying to generate an array from jQuery :checked selector

随声附和 提交于 2020-01-16 15:35:57

问题


I have a table like this:

<tr class="row">
    <td class="row-checkbox-delete-row">
        <input tabindex="-1" class="checkbox-delete-row" type="checkbox" />
    </td>
    <td class="row-target">10</td>
    <td class="row-product-id">
        <input class="id-target row-product-id" name="lineItem[0].originalInput" type="text" data-ajax-line-id="1" />
    </td>
    <td class="row-qty">
        <input class="qty-target row-qty" name="lineItem[0].quantity" type="text" value="1" />
    </td>
    <td class="row-description"></td>
    <td class="row-abc6"></td>
    <td class="row-abc8">
        <input readonly tabindex="-1" class="abc8-target row-abc8" name="lineItem[0].abc8" type="text" />
    </td>
    <td class="row-upc"></td>
    <td class="row-ndc"></td>
</tr>
<tr class="row">
    <td class="row-checkbox-delete-row">
        <input tabindex="-1" class="checkbox-delete-row" type="checkbox" />
    </td>
    <td class="row-target">30</td>
    <td class="row-product-id">
        <input class="id-target row-product-id" name="lineItem[0].originalInput" type="text" data-ajax-line-id="2" />
    </td>
    <td class="row-qty">
        <input class="qty-target row-qty" name="lineItem[0].quantity" type="text" value="1" />
    </td>
    <td class="row-description"></td>
    <td class="row-abc6"></td>
    <td class="row-abc8">
        <input readonly tabindex="-1" class="abc8-target row-abc8" name="lineItem[0].abc8" type="text" />
    </td>
    <td class="row-upc"></td>
    <td class="row-ndc"></td>
</tr>
<tr class="row">
    <td class="row-checkbox-delete-row">
        <input tabindex="-1" class="checkbox-delete-row" type="checkbox" />
    </td>
    <td class="row-target">10</td>
    <td class="row-product-id">
        <input class="id-target row-product-id" name="lineItem[0].originalInput" type="text" data-ajax-line-id="3" />
    </td>
    <td class="row-qty">
        <input class="qty-target row-qty" name="lineItem[0].quantity" type="text" value="1" />
    </td>
    <td class="row-description"></td>
    <td class="row-abc6"></td>
    <td class="row-abc8">
        <input readonly tabindex="-1" class="abc8-target row-abc8" name="lineItem[0].abc8" type="text" />
    </td>
    <td class="row-upc"></td>
    <td class="row-ndc"></td>
</tr>

I have a variable statement like this:

var lineids = $(".checkbox-delete-row:checked").parents("td").siblings(".row-product-id").find(".id-target").attr("data-ajax-line-id");

I am trying to collect all the data-ajax-line-id attributes and put them into an array without looping through the entire thing (using something similar to the jQuery selector I have above) but right now it's returning undefined.

What am I doing wrong?


回答1:


Try .map()

var arr = $(".checkbox-delete-row:checked").closest("tr").find(".row-product-id .id-target").map(function() {
    return $(this).attr("data-ajax-line-id");
}).get();


or
var arr = $(".checkbox-delete-row:checked").closest("tr").find("input.id-target").map(function() {
    return $(this).attr("data-ajax-line-id");
}).get();

var arr = $(".checkbox-delete-row:checked").closest("tr").find("input.id-target").map(function() {
    return $(this).data("ajax-line-id");
}).get();


来源:https://stackoverflow.com/questions/20149141/trying-to-generate-an-array-from-jquery-checked-selector

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!