如何检测文本区域值何时被脚本更改?

How to detect when a textarea value is changed by a script?

我想检查一个 textarea 当它里面的值被程序改变时。

代码非常复杂,所以我宁愿使用事件侦听器,也不愿寻找确切的代码行。

我是这样检查的:

$(".edit.btn.btn-default").on("click", function(){
  alert($("#attribute_fabric_en").val());
  $("#attribute_fabric_en").on("input propertychange",function(){
    alert("test");
  });
});

结果:alert($("#attribute_fabric_en").val());打印了一个空字符串,但是后面的textarea会被脚本赋值;但是没有触发侦听器。

至少Chrome 测试无法捕获事件

有一个事件,在jQuery你可以使用$('.textareaId').on('change', console.log($('#textareaId').html()),就这么简单! 编辑: 也许你不想使用 jQuery,click 事件用于 textarea,contentediatble div 等,而 input 事件用于表单元素,例如 textArea.addEventListener('change', function(e){ ... }, false);\n inputElement.addEventListener('input', function(e) { ... }, false);