1

私はJQuery Mobileテーブルを使用しています:

<table data-role="table" id="productTable" data-mode="reflow" class="tablesorter ui-responsive table-stroke my-custom-breakpoint">
                <thead>
                    <tr>
                        <th data-priority="1">
                            ID
                        </th>

                        ...

                    </tr>
                </thead>
                <tbody>
                    <asp:Repeater ID="productRepeater" runat="server">
                        <ItemTemplate>
                            <tr class="item">
                                <th class="id">
                                    <%# Eval ("ID")%>
                                </th>

                                ...

                            </tr>
                        </ItemTemplate>
                    </asp:Repeater>
                </tbody>
            </table>

今、すべての ID フォームを抽出して、以下の JavaScript の配列に入れようとしています。

function viewSelectedDocuments() {
    var selectedIDs = [];    
    $("tr.item").each(function () {  
        var id = $(this).children().eq(0).text();
        var id = $.trim(id);
        alert(id);
        selectedIDs.push(id);
    });
}

この関数は、ボタンがクリックされたときに呼び出されます。しかし、var id私は期待していたものをまったく得ていません。セルからのテキストの代わりに、"1"私が得ていると言います

"ID

                                    1"

はい -$.trim(id);動作していません。

助言がありますか?

ありがとう!

4

1 に答える 1

3

これは、少しの正規表現で簡単に解決できます。

var id = $(this).children().eq(0).text().replace(/\s+/g, " ");

私が提案するのは、あなたの構造を少し改善することです:

<ItemTemplate>
    <tr class="item" data-id='<%# Eval ("ID")%>'>
        <th class="id">
            <%# Eval ("ID")%>
        </th>
        ...
    </tr>
</ItemTemplate>

function viewSelectedDocuments() {
    var selectedIDs = [];    
    $("tr.item").each(function () {  
        var id = $(this).data('id');
        //var id = $.trim(id); <-- no need to trim anymore

        // depending on what your <%# Eval ("ID")%> you might still need regex
        // id = id.replace(/\s+/g, " "); // uncomment if needed
        alert(id);
        selectedIDs.push(id);
    });
}

Regex + jQuerytext()は、舞台裏でいくつかの DOM 操作を行うため、リソースを少し消費しますが、使用data()は多かれ少なかれ属性値の読み取りです。

于 2013-06-06T09:09:39.190 に答える