javascript - 使用 jquery 和 contenteditable 更新值 ="true"

标签 javascript html jquery onchange contenteditable

我有这个 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/

相关文章:

javascript - 通过jquery给li和parents添加active类

javascript - 通过将数字打包到 Javascript(Typescript)结构的数组中,我可以获得什么好处吗?

javascript - 如果 jQuery 语句 |空文本框

html - CSS - 元素溢出/重叠此父 div 但仍保持溢出 :hidden;?

javascript - 您如何从 Police API 获取数据并将其显示在我的 Google map 上?

javascript - 获取相对于文档的元素偏移量,无需 getBoundingClientRect(Webkit bug)

javascript - 在javascript中排队setTimeout

javascript - 如何编辑 Skype 联系我按钮 CSS?

jQuery css 宽度在 IE/Opera/FF 中不起作用,但在 Chrome 中起作用

javascript - Google map v3 标记事件