2013-06-19 62 views
0

這裏有一個表的HTML標記,我需要點擊td的內容,並將其保存到隱藏字段。
JS FIDDLE DEMOJQuery如何獲取第二個和第三個TD內容取決於第四個TD點擊

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) 
}); 
+0

「fetch td content」是什麼意思? – cgatian

+0

這裏有什麼問題,我認爲它工作 – PSR

+0

sorry.i看到我刪除了我的評論 – PSR

回答

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/(無空格)

+0

非常感謝你,早些時候我不知道'.eq()' –

+0

@Sindersindersh乾杯! –

+0

它的作品,但與文本有一些額外的空白空間我怎麼能刪除這些額外的空白() –

1

看看這裏:http://jsfiddle.net/bpGXp/18/

CODE

$("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 
}); 

希望它有幫助

+0

是的,它的作品謝謝:) –

相關問題