我有表,并且表中的数据被PHP动态填充。当我按按钮时,将启动AJAX请求。在我想对Ajax做的事情之后,我得到了产品的新名称。我想用Ajax随附的新数据,用获得"替换"类的<td>
,并且是我按下的按钮最接近的<td>
。
但我无法做到。
@foreach($products as $key => $product)
<tr>
<td>aaa</td>
<td>bbb</td>
<td class="status">{{ $product->name }}</td>
<td>
<button type="button" class="save btn btn-danger" product-id="{{$product->id}}">
<span>Save</span>
</button>
</td>
</tr>
@endforeach
我尝试了很多这样的事情:
$(".save").on('click', function () {
all my ajax here... ,
success: function (data) {
$(this).closest(".status").html(data); // I tried this
$(this).closest("td.status").html(data); // or this
$(this).closest("tr").find(".status").html(data); // or this
$(this).closest("tr").find(".status").html(data); // or this
},
});
还有其他一些事情,但根本没有任何作用。数据仅包含一个简单的字符串。
this
在您的ajax回调中是jqxhr对象,除非您用上下文更改它
然后,您可以使用closest
和find
选择字段
$(".save").on('click', function () {
all my ajax here... ,
context: this,
success: function (data) {
$(this).closest("tr").find(".replace").html(data); // or this
}
});