这个问题在这里已经有了答案:
Using jQuery $(this) with ES6 Arrow Functions (lexical this binding)
(5 个回答)
1年前关闭。
我有这个 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 ,因为他们不能有 this
与他们绑定(bind)的值(value)。使用普通 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/