日期:2014-05-17  浏览次数:20470 次

==jquery如何获取table中某个元素值==
table如下:
<table  > 
  <tr> 
   <td>1</td>
  <td><input type=text  value="数据1" /></td>
  <td><input type=button  onclick="GetTest()" value="获取" /></td>
  </tr> 
  <tr>
   <td>2</td>
   <td><input type=text  value="数据2" /></td>
   <td><input type=button  onclick="GetTest()" value="获取" /></td>
  </tr> 
</table>

table是通过后台动态输出生成,我想通过点击每一行的button,然后获取当前行的另一列的数据值,比如获取上表的点击行的“数据1”、“数据2”,不知如何写GetTest()函数

------解决方案--------------------
<script type="text/javascript">
$(function(){
$("table tr td").each(function(){
$(this).find("[type=button]").click(function(){
alert($(this).parent().parent().find("[type=text]").val());
});
});
});
  </script>

------解决方案--------------------
function GetTest(obj) {
            alert($(obj).parent().prev().children().val());
        }

onclick="GetTest(this)" 
------解决方案--------------------

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <script src="http://code.jquery.com/jquery-2.0.3.min.js" type="text/javascript"></script>
    <title></title>
    <script type="text/javascript">
        $(function () {
            $("input[type=button]").on("click", function () {
                alert($(this).parent().prev().find("input").val());