我有一个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中访问按钮的值?
你需要使用
$(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] 删除。
我来说两句