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

标签 javascript html jquery onchange contenteditable

这个问题在这里已经有了答案:





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/

相关文章:

javascript - 为什么 "_=1389258551926"在 ajax 请求中作为查询字符串参数发送?

javascript - nodejs变量静态类型是如何工作的

javascript - 用JavaScript添加零小数

javascript - 在 jQuery 中获取 HTML 文档的总高度

html - 使用 :before 替换单选按钮图像

javascript - 需要帮助了解如何在 TinyMCE 中使用 setContent() 函数

jquery - JavaScript : how to change form action attribute value based on selection?

javascript - 如何只允许在输入字段的开头输入文本,并始终附加文本

javascript - Angular 使用从 Assets 文件夹导入

php - html5 php 视频上传和显示 - 如何为特定上传添加评论