0

这里的 html 标记有一个表格,我需要在点击时获取 td 的内容并将其保存到隐藏字段中。
JS 小提琴演示

html标记:

<table>
    <tr style="color:#333333;background-color:#F7F6F3;" class="odd">
        <td style="width:70px;" class="  sorting_1">1</td>
        <td class=" "> <span id="ctl00_ContentPlaceHolder1_gvworkcategory_ctl02_lblworkcategory">Customer Relations</span> 
        </td>
        <td>one</td>
        <td align="center" style="width:80px;" class=" ">
            <img atl="edit" style="border-width:0px;cursor:pointer" src="images/edt1.png" class="update_1" title="Edit" id="ctl00_ContentPlaceHolder1_gvworkcategory_ctl02_Image1">
        </td>
        <td class=" "><a href="abc.aspx?id=1">View Details</a> 
        </td>
    </tr>
    <tr style="color:#333333;background-color:#F7F6F3;" class="odd">
        <td style="width:70px;" class="  sorting_1">2</td>
        <td class=" "> <span id="ctl00_ContentPlaceHolder1_gvworkcategory_ctl02_lblworkcategory">Marketing </span>

        </td>
        <td>two</td>
        <td align="center" style="width:80px;" class=" ">
            <img atl="edit" style="border-width:0px;cursor:pointer" src="images/edt1.png" class="update_2" title="Edit" id="ctl00_ContentPlaceHolder1_gvworkcategory_ctl02_Image1">
        </td>
        <td class=" "><a href="abc.aspx?id=3">View Details</a>

        </td>
    </tr>
    <tr style="color:#333333;background-color:#F7F6F3;" class="odd">
        <td style="width:70px;" class="  sorting_1">3</td>
        <td class=" "> <span id="ctl00_ContentPlaceHolder1_gvworkcategory_ctl02_lblworkcategory">General</span>

        </td>
        <td>three</td>
        <td align="center" style="width:80px;" class=" ">
            <img atl="edit" style="border-width:0px;cursor:pointer" src="images/edt1.png" class="update_3" title="Edit" id="ctl00_ContentPlaceHolder1_gvworkcategory_ctl02_Image1">
        </td>
        <td class=" "><a href="abc.aspx?id=3">View Details</a>

        </td>
    </tr>
</table>
<input id="hdid" type="hidden" />
<input id="hdcol2" type="hidden" />
<input id="hdcol3" type="hidden" />

JavaScript:

$("img[class^='update_']").live('click', function (event) {
    var getId = $(this).attr("class")
    $("#hdid").val(getId);
    alert(getId)
});
4

2 回答 2

2
$(this).closest('tr').find('td').eq(1).text();

$(this).closest('tr').find('td').eq(2).text();

演示-----> http://jsfiddle.net/bpGXp/19/

更新了一个-----> http://jsfiddle.net/bpGXp/21/ (没有空格)

于 2013-06-19T12:08:47.407 回答
1

看看这里:http: //jsfiddle.net/bpGXp/18/

代码

$("img[class^='update_']").live('click', function (event) {
    var tr = $(this).closest("tr");
    var tds = $(tr).find("td");
    alert($(tds[0]).text() + " | " + $(tds[1]).text() + " | " + $(tds[2]).text());

    //save col1, col2 and col3 values where you need it
});

希望能帮助到你

于 2013-06-19T12:09:26.573 回答