在我的 Backbone 模型的 View 中,我有:
events: {
'change textarea' : 'changeContentTextarea',
...
当用户在文本区域中手动输入文本时,它会触发并执行以下操作:
this.model.set('content', this.$el.find('textarea').val());
但是当程序中的内容更改时它不会触发(使用 jQuery):
txtarea.val(finalText);
因此模型的属性不会更新,只有文本区域中的文本发生变化。
我可以绑定(bind)任何事件来解决这个问题吗? 或者如何在使用 jQuery 更改内容后触发该事件?
最佳答案
来自https://api.jquery.com/change/
Note: Changing the value of an input element using JavaScript, using .val() for example, won't fire the event.
所以你必须自己触发change
事件
$("textarea").val(finalText).trigger("change");
关于javascript - 当代码操作输入时,主干 View 更改事件不会触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35695906/