javascript - 获取表中更改的特定元素

标签 javascript jquery backbone.js

我试图获取输入字段表中的特定值,但我总是获取第一个元素而不是单击/更改的元素。我正在使用backbone.js 从每个模型创建表。该表是单独创建的。必须更改/修改的部分在代码“模型 View ”中标记。

这是模型:

DiagnoseApp.Models.Param_4_5_item   = Backbone.Model.extend({
    defaults: {
        data_type: "",
        val_param_4_5: [], //[1,2,3,4,..]
        Amount:"",
        Desc_D:"", 
        Id:"",
        Number:"",
        Type:"",
        Unit:""  
    }
}); 

这是“模型 View ”部分,我想在其中获取特定元素:

模型 View

    DiagnoseApp.Views.Param_5_item   = Backbone.View.extend({
        tagName:    'tr', 
        newTemplate: _.template($('#item-template5').html()), 
        initialize: function() {  this.render();  },
        render: function() {

            for(var i=0; i<500;i++){ //table with 500 colums initalize with 0s
                var getValonIndex = this.model.attributes.val_param_4_5[i];
                this.$el.append('<tr>'+this.newTemplate({Number:i, val_param_4_5_on_Index:getValonIndex})+'</tr>');
            }
        return this;
        },
        events: {
            'blur .edit' :      'editParam',
            'keypress .edit' :  'updateOnEnter'
        },
        updateOnEnter: function(e){
            if(e.which == 13){    
              this.editParam();    
            }
          },
        editParam: function(){

//*********************************************************************
//******************** What do I need to change here to get the current input field and not the first one??
            this.input = this.$('.edit'); //THIS IS PROBABLY WRONG??!!?

            var newValue = this.input.val().trim();
            alert(newValue); 
//*********************************************************************
            //Do some stuff..
        }
    }); 

这是动态生成的表格:

 <section id="main"  >
      <table id="table_param1"><thead id="table_thead_param1"><tr id="head_param1"><th>Nr: </th><th>Value</th></tr></thead><tbody id="body_param1">
    <tr> 
    <td>0</td> <td><input type="text" class="edit" id="new-value" id_nummer="0" value="0"></td>
    </tr>
    <tr> 
    <td>1</td> <td><input type="text" class="edit" id="new-value" value="0"></td></tr>
    <tr> 
    <td>2</td> <td><input type="text" class="edit" id="new-value" value="0"></td>
    </tr>
    <tr> 
    <td>3</td> <td><input type="text" class="edit" id="new-value"  value="0"></td>
    </tr>
....
         </table>    
    </section>

以下是表格中每列的模板:

<script type="text/template" id="item-template5"> 
    <td><%- Number %></td>
    <td><input type="text" class="edit" id="new-value" id_nummer="<%- Number %>" placeholder=0 size=8 value="<%- val_param_4_5_on_Index %>"></td>
</script> 

最佳答案

尝试使用此代码获取目标元素:-

    editParam: function(e){
        this.input = $(e.target); 
        var newValue = this.input.val().trim();
        alert(newValue);

    }

关于javascript - 获取表中更改的特定元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32456147/

相关文章:

javascript - 选择.change()

javascript - 如何使用 Spectron 访问客户端窗口 javascript 全局变量

jquery - html表格的对齐问题

javascript - 在下拉列表 1 中选择某项后启用第二个下拉列表

javascript - SVG <g> 标签获取其 &lt;title&gt; 内容 JS/jQuery

javascript - Backbone - 通过 Controller 中的一次获取调用填充多个模型

javascript - 为什么开发工具不理解闭包?

javascript - 模块中的模拟导出函数

javascript - Backbone.js:将值从 Collection 传递到每个模型

backbone.js - 如何用 Backbone-Relational 表示 UML 关系?