Javascript get.attr(';id';).length;childer输入对焦点的影响

Javascript get.attr(';id';).length;childer输入对焦点的影响,javascript,jquery,Javascript,Jquery,我正在尝试使用$('.keyboard').keypad()在.keyboard上添加小键盘以动态插入行。我正在插入带有$('button[id^=“product”]”的行。如果我在这之后尝试,请单击()。它现在正在工作 var count = 1; $('button[id^="product"]').click(function () { //adding row to table : WORKING FINE var leng=$(this).attr('id').length; var

我正在尝试使用
$('.keyboard').keypad()在
.keyboard
上添加小键盘以动态插入行。我正在插入带有
$('button[id^=“product”]”的行。如果我在这之后尝试,请单击()。它现在正在工作

var count = 1;
$('button[id^="product"]').click(function () {
//adding row to table : WORKING FINE
var leng=$(this).attr('id').length;
var last = $(this).attr('id').substr(leng-4);
var newTr = $('<tr id="row_'+ count + last +'"></tr>');
newTr.html('<td width="29px" style="text-align:center;"><button class="del_row" id="'+ count + last +'" value="'+ item_price +'"> x </button></td><td width="144px"><input type="text" class="code" name="product'+ count +'" value="'+ prod_name +'" /></td><td width="44px" style="text-align:center;"><input class="keyboard" name="quantity'+ count +'" type="text" value="1" id="'+ quan +'"/></td><td style="width: 76px; padding-right: 10px; text-align:right;"><input type="text" class="price" name="price'+ count +'" value="'+ item_price +'" id="'+ pric +'"/></td>');

newTr.appendTo("#saletbl");

count++;         
});

$('.keyboard').keypad({
var leng=$(this).attr('id').length;
var last = $(this).attr('id').substr(leng-4);
}); //not working
如果我在
$('button[id^=“product”])内尝试。单击()
它正在工作并显示小键盘

var count = 1;
$('button[id^="product"]').click(function () {
//adding row to table : WORKING FINE

$('.keyboard').keypad({
var leng=$(this).attr('id').length;
var last = $(this).attr('id').substr(leng-4);
}); // working but variable are being changed with parent

count++;         
});

我的问题是,当我在主函数中添加此变量时,
keypad()
中的变量会随着主函数的变化而变化。如果数量改变,我需要改变那一行的价格。如何获取
var leng=$(this.attr('id').length用于
键盘
元素?我尝试了
$(this.children().attr('id').length但什么都没有。我需要
var leng=$(this.attr('id').length。非常感谢你的帮助

你不认为
$('.keyboard').keypad({var leng=$(this.attr('id')).length;var last=$(this.attr('id')).substr(leng-4);})
是无效的javascript语句,您正在尝试将javascript语句传递给json对象。我正在获取id的最后4个字符,它工作正常。但要和父母一起改变。我想知道我是否可以在任何其他地方声明keypad(),但不能在$('button[id^=“product”])中声明;这是行不通的。您不能在javascript对象内部声明这样的变量。我发现我可以在javascript函数中声明变量,然后在jquery中调用它。它现在正在工作。我这样用可以吗?
var count = 1;
$('button[id^="product"]').click(function () {
//adding row to table : WORKING FINE

$('.keyboard').keypad({
var leng=$(this).attr('id').length;
var last = $(this).attr('id').substr(leng-4);
}); // working but variable are being changed with parent

count++;         
});