javascript - 当我在呈现 View 后设置输入类型的值时,为什么不触发输入类型的更改事件?

标签 javascript events backbone.js view render

在主干 View 中,我在其事件哈希中添加了输入类型的更改事件,该事件将触发函数并为 View 中的某些元素设置值。现在在 View 渲染中,我使用下划线延迟方法将值设置为输入类型,以便在将 View 附加到 DOM 后设置值。这确实在 View 上设置了输入类型的值,但它不会触发它的更改事件,它应该为 View 内的其他字段设置值。

感谢任何帮助!

SomeView = Backbone.View.extend({
    template: 'example.html',
    events: {
        "change #InputElementId": "UpdateFields"
    },
    renderTemplate: function () {
        var view = this,
            model = view.options.selectedModel;

        podiumApp.fetchTemplate(view.template, function (tmpl) {
            view.$el.html(tmpl(model.toJSON()));
        });
    },
    render: function () {
        var view = this,
        view.renderTemplate();
        _.defer(view.loadInputValue);    `enter code here`   

        return view;
    },
    loadInputValue: function () {

            $('#InputElementId').val(model.get('InputRangeValue'));
    },
    UpdateFields: function(){`enter code here`
    // Some logic`enter code here`
      console.log('Changed event fired');
    });

最佳答案

以编程方式设置值不会触发 change 事件,您必须自己触发它

this.$('#InputElementId').val(model.get('InputRangeValue')).trigger('change');


  SomeView = Backbone.View.extend({
      template: 'example.html',
      events: {
        'change #InputElementId': 'updateFields'
      },
      initialize: function() {
        this.model = this.options.selectedModel;
      },
      renderTemplate: function() {
        podiumApp.fetchTemplate(this.template, function(tmpl) {
          this.$el.html(tmpl(this.model.toJSON()));
        }.bind(this));
      },
      render: function() {
        this.renderTemplate();
        _.defer(this.loadInputValue.bind(this));
        return this;
      },
      loadInputValue: function() {
        var value = this.model.get('InputRangeValue');
        this.$('#InputElementId').val(value).trigger('change');
      },
      UpdateFields: function() {
        // Some logic
        console.log('Changed event fired');
      });

关于javascript - 当我在呈现 View 后设置输入类型的值时,为什么不触发输入类型的更改事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55646591/

相关文章:

javascript - ReactJS - 元素类型无效错误

javascript - 如何将坐标字符串转换为 LatLngBound 对象?

javascript - 如何让 countUp Javascript 包正常工作?

javascript - 如何正确地将循环中的参数传递给多个事件处理程序?

javascript - 将 contenteditable 字段序列化为 JSON 对象以保存到模型的最佳方法是什么?

jquery mobile - 在 iPhone 中加载长 ListView 时页面闪烁

javascript - 如何更改待办事项列表中 future 元素的 css?

objective-c - Mac OS X -- 在最前面的窗口更改时收到通知

MYSQL 在事件调度程序中使用事务

jquery - Jasmine.js V2.0.0 - 读取灯具数据 - TypeError : undefined is not a function at Object