如何通过jQuery获取所选行的列值 [英] how to get values of columns for a selected row through jQuery

查看:48
本文介绍了如何通过jQuery获取所选行的列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里我试图通过jquery获取所选行的特定列的值。以下代码我有两行没有任何id。我尝试了以下方式并获得该列的两个行值相互追加.how仅使用jquery获取该行的值。





here i am trying to fetch values of a particular column for a selected row via jquery.In Following code i have two rows which do not have any id.I tried following way and getting both rows value for that column appending each other.how to get value for that row only using jquery only.


<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script type="text/javascript">

function test(){
var id = $(".use-address").closest("tr").find('td:eq(2)').text();
  alert(id);
}
    </script>

</head>
<body>
<table id="choose-address-table" class="ui-widget ui-widget-content">
  <thead>
    <tr class="ui-widget-header ">
      <th>Name/Nr.</th>
      <th>Street</th>
      <th>Town</th>
      <th>Postcode</th>
      <th>Country</th>
      <th>Options</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td class="nr"><span>50</span>

      </td>
      <td>Some Street 1</td>
      <td>Glas</td>
      <td>G0 0XX</td>
      <td>United Kingdom</td>
      <td>
        <button type="button" class="use-address" onclick="test();">Use</button>
      </td>
    </tr>
    <tr>
      <td class="nr"><span>30</span>
      </td>
      <td>Some Street 2</td>
      <td>Glasgow</td>
      <td>G0 0XX</td>
      <td>United Kingdom</td>
      <td>
          <button type="button" class="use-address" onclick="test();">Use</button>
      </td>
    </tr>
  </tbody>
</table>
</body>
</html>

推荐答案

.use-address)。nearest( tr)。find(' td:eq(2)') 。文本();
alert(id);
}
< / 脚本 >

< / head >
< 正文 >
< table id = choose-address-table class = ui-widget ui-widget-content >
< thead >
< tr < span class =code-attribute> class = ui-widget-header >
< th > 名称/编号< / th >
< th > 街道< / th >
< th > Town < ; / th >
< th > 邮政编码< ; / th >
< th > 国家< ; / th >
< th > 选项< ; / th >
< / tr >
< / thead >
< tbody >
< tr >
< td class = nr > < span > 50 < / span >

< / td >
< td > 某些街道1 < / td >
< td > Glas < / td >
< td > G0 0XX < / td >
< td > 英国< / td >
< td >
< 按钮 type = 按钮 class = use-address onclick = test(); > 使用< / button >
< / td >
< / tr >
< tr >
< td class = nr > < span > 30 < / span >
< / td >
< td > 某些街道2 < / td >
< td > 格拉斯哥< / td >
< td > G0 0XX < / td >
< td > 英国< / td >
< td >
< 按钮 type = button class = use-address onclick = test(); > 使用< / button >
< span class =code-keyword>< / td > ;
< / tr >
< / tbody >
< / table >
< / body >
< / html < span class =code-keyword>>
(".use-address").closest("tr").find('td:eq(2)').text(); alert(id); } </script> </head> <body> <table id="choose-address-table" class="ui-widget ui-widget-content"> <thead> <tr class="ui-widget-header "> <th>Name/Nr.</th> <th>Street</th> <th>Town</th> <th>Postcode</th> <th>Country</th> <th>Options</th> </tr> </thead> <tbody> <tr> <td class="nr"><span>50</span> </td> <td>Some Street 1</td> <td>Glas</td> <td>G0 0XX</td> <td>United Kingdom</td> <td> <button type="button" class="use-address" onclick="test();">Use</button> </td> </tr> <tr> <td class="nr"><span>30</span> </td> <td>Some Street 2</td> <td>Glasgow</td> <td>G0 0XX</td> <td>United Kingdom</td> <td> <button type="button" class="use-address" onclick="test();">Use</button> </td> </tr> </tbody> </table> </body> </html>


Remove onclick function try this


body)。on(' click'' .use-address' function (){
("body").on('click', '.use-address', function() {


这篇关于如何通过jQuery获取所选行的列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆