使用javascript / jquery选择表中的元素

时间:2012-11-05 19:11:56

标签: javascript jquery

我正在尝试在表格中为每一行选择一个ID。

这是我的桌子(目前用浏览器检查)。

<table id="sort">
            <thead>
                <tr>
                    <th></th>
                    <th></th>
                    <th></th>
                    <th>Pri </th>
                </tr>
            </thead>

                        <tbody style="cursor: n-resize; " class="ui-sortable"><tr class="priorityRow">
                            <td id="tableDragSort">
                                <a class="deleteLink" href="#" rel="17904" title="">
                                 <img src="/Content/Images/Garage/DeleteButton.png"></a>
                            </td>
                            <td>
                                <img id="MainContent_MainColContent_ImageRepeater_CarImg_0" class="17904" src="" style="height:45px;width:60px;">
                            </td>
                            <td>
                                <input name="ctl00$ctl00$MainContent$MainColContent$ImageRepeater$ctl00$txtText" type="text" value="hjjj" id="MainContent_MainColContent_ImageRepeater_txtText_0">
                            </td>
                            <td class="picturePriority">
                                1
                            </td>
                        </tr><tr class="priorityRow" style="opacity: 1; z-index: 0; ">
                            <td id="tableDragSort" style="width: 22px; ">
                                <a class="deleteLink" href="#" rel="17903" title="">
                                 <img src="/Content/Images/Garage/DeleteButton.png"></a>
                            </td>
                            <td style="width: 60px; ">
                                <img id="MainContent_MainColContent_ImageRepeater_CarImg_1" class="17903" src="" style="height:45px;width:60px;">
                            </td>
                            <td style="width: 153px; ">
                                <input name="ctl00$ctl00$MainContent$MainColContent$ImageRepeater$ctl01$txtText" type="text" value="dd" id="MainContent_MainColContent_ImageRepeater_txtText_1">
                            </td>
                            <td class="picturePriority" style="width: 21px; ">
                                2
                            </td>
                        </tr><tr class="priorityRow" style="opacity: 1; z-index: 0; ">
                            <td id="tableDragSort" style="width: 22px; ">
                                <a class="deleteLink" href="#" rel="17895" title="">
                                 <img src="/Content/Images/Garage/DeleteButton.png"></a>
                            </td>
                            <td style="width: 60px; ">
                                <img id="MainContent_MainColContent_ImageRepeater_CarImg_2" class="17895" src="" style="height:45px;width:60px;">
                            </td>
                            <td style="width: 153px; ">
                                <input name="ctl00$ctl00$MainContent$MainColContent$ImageRepeater$ctl02$txtText" type="text" value="wggw" id="MainContent_MainColContent_ImageRepeater_txtText_2">
                            </td>
                            <td class="picturePriority" style="width: 21px; ">
                                3
                            </td>
                        </tr>
                  </table>

179041790317895<a rel=""><img class="">的ID是我要在我的javascript方法中选择的ID在这里:

$("#sort .priorityRow").each(function ()
    {
        var index = $(this).index() + 1;


        var id = /* SELECT ID HERE */

        console.log(id);
        $(this).find(".picturePriority span").text(index);
        SetPicturePriority(id, index);
    });
}

我不关心我选择id的方式,只要它有效。

任何人都可以提供帮助吗? :)

2 个答案:

答案 0 :(得分:1)

你可以像这样得到身份

var id =$(this).attr("id");

答案 1 :(得分:1)

听起来像也许你正试图这样做:

$("#sort .priorityRow").each(function () {
    var rel = $(this).find("a").first().attr("rel"),
        index = this.rowIndex + 1;

    $(this).find(".picturePriority").text(index);

    SetPicturePriority(rel, index);
});