[英]Jquery input fields incrementation with table row cloning
I have an issue with a script I'm working on which has a set of columns in a table and 2 inputs field per row. 我正在处理的脚本存在问题,该脚本在表中具有一组列,每行2个输入字段。
The first input field is <input type="text" id="code_x" name="code_x" class="form-control" onchange="oncode_changex(this.value)" />
and this one will trigger oncode_changex(this.value) when onchange. 第一个输入字段是
<input type="text" id="code_x" name="code_x" class="form-control" onchange="oncode_changex(this.value)" />
,这将触发oncode_changex(this。值)。
The second input field is <input type="text" id="qte_x" name="qte_x" class="form-control" onkeyup="onqte_changex(this.value)" />
and this one will trigger another function onqte_change(this.value) when onkeyup. 第二个输入字段是
<input type="text" id="qte_x" name="qte_x" class="form-control" onkeyup="onqte_changex(this.value)" />
,这将触发另一个函数onqte_change( this.value)。
My JQuery script will clone the row and increment it's id's, names and event attributes by 1 everytime a row is added. 我的JQuery脚本将克隆该行,并在每次添加一行时将其ID,名称和事件属性加1。
The cloning and incrementing works just perfect. 克隆和增量工作非常完美。
The issue I have is both event attributes are duplicated inside both input fields. 我遇到的问题是两个输入字段中的两个事件属性都重复。 Usually the script should increment the onchange inside the "#code_x" and the onkeyup inside the "#qte_x" NOT in both textfields
通常,脚本应该在两个文本字段中不增加“ #code_x”内部的onchange和“ #qte_x”内部的onkeyup
Here's the Jquery I have : 这是我拥有的Jquery:
jQuery.fn.addClone = function() { jQuery.fn.addClone = function(){
return this.each(function() {
// get row for cloningg
var row = $(this).parents('tr');
var parent = {};
// use tbody or table parent
if ( $(row).parents('tbody').length>0) {
parent = $(row).parents('tbody');
} else {
parent = $(row).parents('table');
}
// inject clone
var copy = $(row).clone();
$(copy).addClass('sadey');
$(copy).addClass('isclone');
$(parent).append( copy );
// remove last td and replace with remove html
$('.sadey').children('td:last').remove();
var id = ($('.isclone').length + 1);
$('.sadey').append('<td><button class="btn btn-block btn-danger" id="clickme" onclick="$(this).killClone' + id +'()">Retirer</button></td>');
// increment all ids and names
$('.sadey').find('*').each(function() {
var tempId = $(this).attr('id');
if (typeof tempId != 'undefined' && tempId!='') {
$(this).attr('id',tempId + '_' + id);
$(this).attr('onchange','oncode_changex' + id + "(this.value)");
$(this).attr('onkeyup','onqte_changex' + id + "(this.value)");
}
var tempName = $(this).attr('name');
if (typeof tempName != 'undefined' && tempName!='') {
$(this).attr('name',tempName + '_' + id);
}
});
// remove active tag
$('.sadey').removeClass('sadey');
});
}; };
and here's HTML : 这是HTML:
<tr>
<td>
<div class="form-group">
<input type="text" id="code_x" name="code_x" class="form-control" onchange="oncode_changex(this.value)" />
</div>
</td>
<td>
<div class="form-group">
<input type="text" name="qte_x" id="qte_x" class="form-control" value="1" onkeyup="onqte_changex(this.value)" />
</div>
</td>
<td colspan="3"><div id="txtHint3_19" style="width: 100%"> </div></td>
<td><button type="button" class="btn btn-primary btn-sm" onClick="$(this).addClone();">Ajouter un autre article</button></td>
</tr>
I am fairly new to JQuery so if anybofdy can explain to me why the events attributes are incrementing, but also duplicating in both textfields. 我对JQuery还是比较陌生,因此,如果anybofdy可以向我解释事件属性为何在递增,而且在两个文本字段中都重复的原因。
RESULT once cloned : 结果一旦克隆:
<tr class="isclone">
<td>
<div class="form-group">
<input id="code_x_2" class="form-control" type="text" onchange="oncode_changex2(this.value)" name="code_x_2" onkeyup="onqte_changex2(this.value)"></input>
</div>
<div class="form-group">
<input id="qte_x_2" class="form-control" type="text" onkeyup="onqte_changex2(this.value)" value="1" name="qte_x_2" onchange="oncode_changex2(this.value)"></input>
</div>
</td>
<td colspan="3"></td>
<td></td>
We can see both onkeyup and onchange event in both textfields. 我们可以在两个文本字段中同时看到onkeyup和onchange事件。 They should be separate....
它们应该分开...
Thank you for your help! 谢谢您的帮助!
From what I understand, you are indeed setting both attributes on both inputs with : 据我了解,您确实在两个输入上都设置了两个属性:
$(this).attr('id',tempId + '_' + id);
$(this).attr('onchange','oncode_changex' + id + "(this.value)");
$(this).attr('onkeyup','onqte_changex' + id + "(this.value)");
You may want to check if the input has the attribute before setting it, with something like : 您可能想要在设置输入之前检查输入是否具有该属性,例如:
if ($(this).attr('onchange')) {
$(this).attr('onchange','oncode_changex' + id + "(this.value)");
}
And do the same for the other attribute. 并对其他属性执行相同的操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.