Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 动态创建输入类型=';文本';然后,将每个框中的值添加到Backbone.js数组中_Javascript_Arrays_Dynamic_Backbone.js_Input - Fatal编程技术网

Javascript 动态创建输入类型=';文本';然后,将每个框中的值添加到Backbone.js数组中

Javascript 动态创建输入类型=';文本';然后,将每个框中的值添加到Backbone.js数组中,javascript,arrays,dynamic,backbone.js,input,Javascript,Arrays,Dynamic,Backbone.js,Input,我想知道如何基于type='number'框创建多个文本框…因此,一旦有人向数字框添加1,另一个文本框字段将附加到backbone.js视图…然后,一旦有人在这些文本框中输入值,将每个值添加到主干模型数组中的一个点。下面是一些代码: <label for='choices'># Choices for Students</label> <input type='number' name='choices' step=1 /> init

我想知道如何基于type='number'框创建多个文本框…因此,一旦有人向数字框添加1,另一个文本框字段将附加到backbone.js视图…然后,一旦有人在这些文本框中输入值,将每个值添加到主干模型数组中的一个点。下面是一些代码:

    <label for='choices'># Choices for Students</label>
    <input type='number' name='choices' step=1 />

    initialize: function(opts) {
    this.model = new QuestionCreateModel({
        mode: null,
        choices: ['A', 'B', 'C'],
        Question: "Question goes here",
        MaxChoices: 0,
        MinChoices: 0,
        WordLimit: 0,
        CharLimit: 0,
    }),
学生的选择 初始化:函数(opts){ this.model=新问题CreateModel({ 模式:空, 选项:['A','B','C'], 问题:“问题到此为止”, MaxChoices:0, MinChoices:0, 字数限制:0, 字符限制:0, }), 正如您所看到的,我希望输入type='number',然后加载文本框,以便为主干模型中的choices数组赋值

谢谢你的帮助


-斯图,我想你的密码不够用

首先,您需要一个集合和一个模型

然后创建视图,该视图将侦听集合的添加、删除、更改或重置事件。如果这样做,视图将处理这些事件并呈现您所说的应该呈现的内容

myView = Backbone.View.extend({
   initialize : function() {
       this.collection = this.options.collection || new myCollection();
       this.collection.on("add remove reset change", this.render, this)
   },
   events : {
       "change [type='number']" : "numberChanged"
   },
   numberChanged : function(ev) {
       var $el = $(ev.target || ev.srcElement);
       var model = $el.data("model");
       model.set("selectedChoice", $el.val());
   },
   render : function() {
       this.$el.empty();
       this.collection.each(function(model) {
           $("<yourinput>").data("model", model)
               .appendTo(this.$el);
       }, this);
   }
});
var myModel = Backbone.Model.extend({
   initialize : function() {
      this.on("change:selectedChoice", this.onChoiceChanged, this);
   },
   onChoiceChanged : function(model,value) {
      // from here you know, that a value was selected, you now
      // can say the collection, it should create a new model
      if (this.collection) this.collection.push();
      // this will trigger a "add" event and your "view" will react and rerender.
   }
});

var myCollection = Backbone.Collection.extend({
   model : myModel
});