0

私はこの方法を持っています:

$("#btnupdateofficeapprovers").click(function () {
        var checkedInvoiceLineIds = $(":checked").attr("data-invoicelineid");

        checkedInvoiceLineIds.each(function (index) {
            alert(index + ': ' + $(this).text());
        });
    });




<table>
        @foreach (var item in Model.Invoice.InvoiceLines) {
            <tr class="subheader">
                <td>
                    <input type="checkbox" class="invoicelinecombobox" data-invoicelineid=@item.InvoiceLineId >
                </td>
            </tr>

        }
    </table>

<div id="updateapproversdiv">
    @Html.DropDownListFor(modelItem => Model.Invoice.ApprovedForPaymentUserId, Model.OfficeApprovers, new { @class = "officeapproverddl" })
    <input type="button" id="btnupdateofficeapprovers" class="button invisibleforprint" value="Update" />
</div>

私がやろうとしているのは、すべてのinvoicelineidを取得してコレクションに入れることです。

次に、リスト内の各 ID を実行して、アラートに表示したいと考えています。

問題は、これが大きな例外をスローしていることです。誰でも修正方法を知っていますか?これを行う方法?

4

3 に答える 3

1

jQueryセレクターの「属性あり」を使用するだけです:http://api.jquery.com/has-attribute-selector/

jsFiddle: http: //jsfiddle.net/NF5Ss/1/

$("#btnupdateofficeapprovers").click(function () {
    var checkedInvoiceLineIds = $(":checked[data-invoicelineid]");

    console.log(checkedInvoiceLineIds);

    checkedInvoiceLineIds.each(function(index) {
       alert(index + ': ' + $(this).data('invoicelineid'));
    });
});​
于 2012-04-11T08:52:24.323 に答える
0

.eachはjqueryオブジェクトを反復処理します。チェックされたInvoiceLineIdsに、文字列で.eachを持たない.attr()を割り当てます。

= $(':checked')にしてから、.eachでその.attrにアクセスします。

于 2012-04-11T08:48:05.120 に答える
0

attr()文字列を返します。で文字列を繰り返すことはできませんeach()。これはおそらくあなたがやろうとしていることです:

$("#btnupdateofficeapprovers").click(function () {
    var ids = $("input:checked").attr("data-invoicelineid").split(' ');
    $.each(ids, function(i, v) {
        alert(i + ': ' + v);
    });
});

編集:@Armatusが提案したように行うこともできます:

$("#btnupdateofficeapprovers").click(function () {
    var $els = $("input:checked");
    $els.each(function(i){
        alert(i + ': ' + $(this).attr('data-invoicelineid'));
    });
});
于 2012-04-11T08:46:14.060 に答える