在Jquery中获得TD的价值

时间:2014-11-26 06:39:51

标签: javascript jquery

我的网页上有多个tr,其中包含tds。现在每个tr都有一个bd_cl的td我希望得到{{1}的值点击链接

HTML

bd_cl

的jQuery

<tr>
    <td>121</td>
    <td>111</td>
    <td>11</td>
    <td bd_cl="PE">Address</td>
    <td>
        <a class="edit" title="Edit User" onclick="update(this)" href="javascript:void(0);"><img border="0" src="/icon_edit.gif"></a>
    </td>
</tr>
<tr>
    <td>121</td>
    <td>111</td>
    <td>11</td>
    <td bd_cl="AD">Name</td>
    <td>
        <a class="edit" title="Edit User" onclick="update(this)" href="javascript:void(0);"><img border="0" src="/icon_edit.gif"></a>
    </td>
</tr>

但我无法获得价值。在这个问题上帮助我。

6 个答案:

答案 0 :(得分:1)

您有错误的选择器,用于定位点击的锚元素父级以前的td元素。使用正确的选择器和.attr("bd_cl")来获取值:

function update (objref){
  var $parenttdobj = $(objref).parent();
  alert($parenttdobj.prev().attr("bd_cl"));
}

<强> Working Demo

答案 1 :(得分:0)

您可以使用以下代码

$('td').each(function(){
   if($(this).hasAttr('bd_cl')){
      alert($(this).text());
   }
});

答案 2 :(得分:0)

您的功能名称中有拼写错误。 此外,您错过了table代码。

如果您想获取属性值,请使用.attr('bd_cl')

function update (objref){
    var $parenttrobj = $(objref).closest('tr').find('td[bd_cl]');
    alert($parenttrobj.attr("bd_cl"));
}

如果您想获得td内容:

function update (objref){
    var $parenttrobj = $(objref).closest('tr').find('td[bd_cl]');
    alert($parenttrobj.text());
}

function update(el) {
 var td = $(el).parent().prev();
 alert(td.attr('bd_cl'));
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
    <td>121</td>
    <td>111</td>
    <td>11</td>
    <td bd_cl="PE">Address</td>
    <td>
        <a class="edit" title="Edit User" onclick="update(this)" href="javascript:void(0);"><img border="0" src="//placehold.it/32x32"></a>
    </td>
</tr>
<tr>
    <td>121</td>
    <td>111</td>
    <td>11</td>
    <td bd_cl="AD">Name</td>
    <td>
        <a class="edit" title="Edit User" onclick="update(this)" href="javascript:void(0);"><img border="0" src="//placehold.it/32x32"></a>
    </td>
</tr>
</table>

答案 3 :(得分:0)

$(".edit").click(function update() {
    $parenttrobj = $(this).closest('tr');
    alert($parenttrobj.find("[bd_cl]").attr("bd_cl"))
});

<强> DEMO

答案 4 :(得分:0)

在jquery中使用find

var $parenttrobj = $(objref).closest('tr');

alert($parenttrobj.find("td").attr("bd_cl"));

答案 5 :(得分:0)

我建议稍微修改你的方法,以摆脱内联事件处理程序,使用jQuery来分配点击处理功能:

function update() {
  // 'this' here is the element that was interacted with
  // at the point of event-handling being assigned.

  // find the closest ancestor <tr> element,
  // find the the <td> elements that have a 'bd_cl' attribute:
  var cell = $(this).closest('tr').find('td[bd_cl]');

  // use console.log() rather than 'alert' to log the retrieved text:
  console.log(cell.text());
}

// adds a click-handling function to the <a class="edit"> elements
// that are within <td> elements. jQuery will pass the clicked
// element to the assigned function as 'this':
$('td a.edit').on('click', update);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tbody>
    <tr>
      <td>121</td>
      <td>111</td>
      <td>11</td>
      <td bd_cl="PE">Address</td>
      <td>
        <a class="edit" title="Edit User" href="#">
          <img border="0" src="/icon_edit.gif">
        </a>
      </td>
    </tr>
    <tr>
      <td>121</td>
      <td>111</td>
      <td>11</td>
      <td bd_cl="AD">Name</td>
      <td>
        <a class="edit" title="Edit User" href="#">
          <img border="0" src="/icon_edit.gif">
        </a>
      </td>
    </tr>
  </tbody>
</table>

当您尝试查找<td>元素中包含的文字时,明显 <input /><select>元素时,它没有{{1它可用的方法;而必须使用val()

此外,虽然这可行,但由于您使用的自定义属性,值得指出您的HTML无效。但是,在HTML 5下,您可以访问有效的text()自定义属性,这些属性将在HTML 5文档类型(data-*)下进行验证,但它仍然与HTML 4.x一样无效(尽管向后兼容) 。因此,要使用<doctype html>属性:

data-*
function update() {
  var cell = $(this).closest('tr').find('td[data-bd_cl]');
  console.log(cell.text());
}

$('td a.edit').on('click', update);

参考文献: