我有这个 HTML:
<tr role="row" class="odd">
<td class="sorting_1">Deluxe Junior Suite</td>
<td contenteditable="true" class="update_rate" data-id="46">0.00</td>
</tr>
当用户更改表格单元格中的值时,我想使用 jquery 更新价格,因此我写道:
$('.update_rate').on('input', (e) => {
var price = $(this).html();
var id = $(this).data('id');
var data = {
_token: "{{ csrf_token() }}",
id: id,
price: price,
};
console.log(data);
$.ajax({
type: "POST",
url: '/update_rate',
dataType: "json",
data: data,
success: function (data)
{
if (data.status == '200') {
console.log('updated');
}
},
error: function(data)
{
console.log(data);
}
});
});
但我收到一个错误:
jquery.min.js:2 未捕获的类型错误:无法读取 null 的属性“createDocumentFragment”
怎么解决这个问题?有什么问题吗?
最佳答案
您不能使用arrow function ,因为它们不能绑定(bind) this
值。使用普通function expression相反。
$('.update_rate').on('input', function(e){
var price = $(this).html();
var id = $(this).data('id');
var data = {
_token: "{{ csrf_token() }}",
id: id,
price: price,
};
console.log(data);
$.ajax({
type: "POST",
url: '/update_rate',
dataType: "json",
data: data,
success: function (data)
{
if (data.status == '200') {
console.log('updated');
}
},
error: function(data)
{
console.log(data);
}
});
});
关于javascript - 使用 jquery 和 contenteditable 更新值 ="true",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62564420/