gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 08:56:50 25 4
gpt4 key购买 nike

我试图获取输入字段表中的特定值,但我总是获取第一个元素而不是单击/更改的元素。我正在使用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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com