如何在选中复选框时突出显示表行(单击按钮时)
How to make a table row highlight if the checkbox is checked (on button click)?
我正在创建一个Jquery移动页面,其中包括一个表。该表将显示用户。表行包括第一个单元格中的复选框,后面是用户信息。在表的底部有一个按钮(Edit)。当这个按钮被点击时,我希望选中复选框的行被突出显示,并且行中的单元格是可编辑的。
我想在点击编辑按钮时突出显示/使行可编辑,当
- a)选中行复选框。
表已经使用JavaScript循环创建(函数在页面加载时加载)。
var UsersHTML = "<table class='full_width_table info_table_style ui-body-d ui-shadow table-stripe ui-responsive'>" +
"<thead>" +
"<tr class='ui-bar-b schedule_row'>" +
"<th></th>" +
"<th>UserID</th>" +
"<th>First Name</th>" +
"<th>Surname</th>" +
"<th>Home Location</th>" +
"</tr>" +
"</thead>" +
"<tbody>";
for (s = 0; s < 15; s++) {
//contenteditable='true'
UsersHTML += "<tr class='schedule_row'>" +
"<td class='user_check_cell'>" +"<input type='checkbox'>" + "</td>" +
"<td> " + + "</td>" +
"<td>" + + "</td>" +
"<td>" + + "</td>" +
"<td>" + + "</td>" +
"<td>" + + "</td>" +
"<td align='right'";
UsersHTML += "</td></tr>";
}
UsersHTML += "</tbody></table>";
$("#usersContent").html(UsersHTML);
我使用了一个div来显示页面上的表格:
<div id="usersContent">
</div>
任何帮助将是伟大的!
您可以使用事件委托。
$('#userContent').on('click', '.user_check_cell input[type=checkbox]', function () {
var $input = $(this);
$input.closest('tr').toggleClass('highlighted', $input.prop('checked'));
});
这将事件侦听器添加到#userContent
并侦听其上的事件,如果触发该单击事件的元素与on
中的选择器匹配,则该事件将继续进行。
toggleClass
只是添加类"高亮",当被点击的输入被检查。
试试这个: