2016-02-21 49 views
0

这让我再次在脑海中死去。连续出现td

问题: 试图在与选定按钮相同的行中获取隐藏td的值。我的代码找到了两个隐藏的tds的值,我只想要按下按钮的同一行的隐藏td的值。

在此先感谢

HTML

<tr> 
    <td class="rowid" hidden>1</td> 
    <td >data</td> 
    <td ><button>process</button></td> 
</tr> 
<tr> 
    <td class="rowid" hidden>2</td> 
    <td >data</td> 
    <td ><button>process</button></td> 
</tr> 

jQuery的

$j("Button").on("click",function(){  
    var strRwId = $j("td.strRowId").text(); 
    $j("td.strRowId").css("background-color", "red"); 
    alert("you pressed the edit button for Row: " + strRwId + "!"); 
}); 
+0

错字这里 - '

+0

Awee来吧现在,这是次要的,我是问在代码的帮助,不会修正我的问题,如果你纠正错字。 – Dewinky

+0

不可以.. @Dewinky ,,我只是指出..只是你要修复它的帖子.. – Lal

回答

0

我认为你正在寻找这样的东西。

$("button").on("click", function() { 
 
    var tr = $(this).closest('tr'); 
 
    var strRwId = tr.find('.rowid').text(); 
 
    tr.css("background-color", "red"); 
 
    alert("you pressed the edit button for Row: " + strRwId + "!"); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table> 
 
    <tr> 
 
     <td class="rowid" hidden>1</td> 
 
     <td>data</td> 
 
     <td><button>process</button></td> 
 
    </tr> 
 
    <tr> 
 
     <td class="rowid" hidden>2</td> 
 
     <td>data</td> 
 
     <td><button>process</button></td> 
 
    </tr> 
 
</table>

1

这可能给你一个想法

$("button").click(function() { 
    var $button = $(this); 
    var $tr = $button.parent("tr"); 
    var $hidden = $tr.find("td[hidden]"); 
    alert("you pressed the edit button for Row: " + $hidden.html() + "!"); 
})