我有一个表格和一个按钮来获取所选行:
<table id="mytable" class="table-striped">
<tbody>
<tr id="1"><td>Test1</td></tr>
<tr id="2"><td>Test2</td></tr>
<tr id="3"><td>Test3</td></tr>
<tr id="4"><td>Test4</td></tr>
<tr id="5"><td>Test5</td></tr>
</tbody>
</table>
<button id="btn">Get Selected Row</button>
当我单击该行时,我将背景颜色设置为红色。
$('#mytable').on('click', 'tbody tr', function (event) {
$(this).addClass('highlight').siblings().removeClass('highlight');
});
function getRow() {
$('table > tbody > tr').find('background-color: red');
}
$('#btn').click(function (e) {
var selrow = getRow();
console.log(selrow);
if (selrow != undefined)
alert(selrow.attr('id'));
else
alert('undefined');
});
问题是当我单击按钮时如何使用 jquery 获取所选行(背景颜色为红色)?
最佳答案
您可以将任何具有 .highlight
类的行与 tr.highlight
相匹配。
如果您想要 jQuery 对象(如果找到匹配项),您需要在 getRow()
函数中返回它:
function getRow() {
return $('table > tbody > tr.highlight');
}
关于javascript - 如何使用 jquery 查找选定的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33298313/