从选定的tr获取特定css类的值

霍恩·霍利(KhorneHoly)

我有一个html表,其中包含多行,所有行的结构都相同。

我可以通过jQuery选择被单击的行,但是现在我想使用位于该行内的css类“ book”来获取某个字段的值。

$(function(){
    $(document).on('click', '.book_listing', function(){
        $(this).css('background', 'red');
        // That's the code I tried to get the val
        console.log($(this).find('.book').val());
    })
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<table>
  <thead>
    <tr>
      <th>Stuff 1</th>
      <th>Stuff 2</th>
      <th>Stuff 3</th>
    </tr>
  </thead>
  <tbody>
    <tr class="book_listing">
      <td><button class="book">The value I want</button></td>
      <td>Other stuff</td>
      <td>and so on</td>
    </tr>
  </tbody>
</table>

但是我不是上面代码中按钮的值。

如何从选定的tr中访问按钮的值?

亚历山大·加吉奇(Aleksandar Gajic)

你需要使用

$(this).find('.book').text()

或者你可以使用

$(this).find('.book').html()

如果使用.text(),则将获得文本;如果使用.html(),则将获得带有标记的文本。例子

<button class="book">The value I <b>want</b></button>

.text() will return: The value I want
.html() will return: The value I <b>want</b>

.val()我用于输入,选择和文本区域。这就是为什么它对您的代码不起作用的原因。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

获取选定类 vuejs 的值

来自分类Dev

通过类 Attribute 从选定的行 <tr> 中获取列值 <td>

来自分类Dev

Angular 2+获取选定值的特定值

来自分类Dev

如何从C#中的选定类中获取变量的值?

来自分类Dev

如何从特定表单中获取选定的单选框值?

来自分类Dev

如何从特定表单中获取选定的单选框值?

来自分类Dev

从jpopupmenu获取选定的值

来自分类Dev

获取更改的选定值

来自分类Dev

从NSTableView获取选定的值

来自分类Dev

msdropdown获取选定的值

来自分类Dev

获取选定的值

来自分类Dev

从选定的表中删除类 -> tr

来自分类Dev

jQuery获取父元素的特定类的值

来自分类Dev

从TR获取TD值

来自分类Dev

从TR获取TD值

来自分类Dev

在<a> angularJS中获取选定的值

来自分类Dev

获取行中的选定值

来自分类Dev

引导多选获取选定的值

来自分类Dev

如何获取选定值的ID?

来自分类Dev

在<a> angularJS中获取选定的值

来自分类Dev

XUL获取选定的MENUITEM的值

来自分类Dev

在 KnockoutJs 中获取选定的值

来自分类Dev

symfony 获取选定的 choiceType 值

来自分类Dev

获取点击TR的TD类?

来自分类Dev

如何使用jQuery中的类获取选定单选按钮和复选框的值?

来自分类Dev

从Bootstrap DropDown中获取具有特定ID的选定项目值

来自分类Dev

使用 Python 2.7 从 tree.view 设置中的选定行获取特定列值

来自分类Dev

我想根据微调器的选定值从特定的数组列表中获取元素

来自分类Dev

如何找到附加了特定类的 tr 并获取每个 td 的详细信息 - jquery