我想一个类添加到同一行的按钮,我点击是在该行的颜色。我用jQuery的“最接近”的功能,但它不没有工作。我也试过.parent等任何人都知道我做错了什么?制作按钮来更改它在
<table class="table table-striped table-bordered table-hover">
<tbody class="">
<tr class=''> //I want to change this class
<td style=''><div class='btn btn-xs btn-success cellButton'></td>
</tr>
</tbody>
$('.cellButton').on('click', function(){
$(this).closest("tr").addClass('info')
}
编辑:我现在意识到我发布的代码应该工作(我发布之前简化它),问题是,因为它是从一个不同的功能指引$(本)。这才是真正的javascript函数:
$('.cellButton').on('click', function(){
var formData = {
'action': 'blabla',
'ID': ID,
};
$.ajax({
type: 'POST',
url: 'includes/jquery-actions.php',
data: formData,
dataType: 'json',
error: function (response) {
alert("error!");
},
success: function (response) {
var row = $(this).parents("tr"); //$(this) here not referencing the HTML button?)
row.addClass('warning');
}
})
}
也许它不工作,因为你没有' )'您的点击功能.. –