我想点击它来更新表格单元格值。我试过以下方式。但所有行都受到影响而不是单行。我给了唯一的索引,但我不知道我哪里出错了。
HTML代码
<html>
<body>
<tbody id="itemtab" style="width:100%;height:200px !important;font-size:12px">
</tbody>
</body>
</html>
Jquery代码
$("body").on('click','#btn',function(e){
var box=$(this).text();
console.log("Box Number to scan item code is: "+box);
/*Check box is already scanned or not*/
$.ajax({
type : 'POST',
url : '@routes.Application.isboxscanned()',
data : {
content:box,
orgname:'Intel'
},
success : function(data) {
console.log("flag"+data)
if(data==2)
{
console.log("Started Scanning through webservice");
var k;
$.ajax({
type : 'POST',
url : '@routes.Application.searchItemcodes()',
data : {
boxnumber : boxnumber,
orgname : 'IN1005'
},
success : function(jsonp) {
$("#main").slideUp();
$("#itemscanning").slideDown();
$("#itemtab").empty();
var array = jsonp.data;
$("#boxnum").text(boxnumber)
k=array.length;
for(i = 0; i < array.length; i++){
console.log(array[i].documentno);
console.log(array[i].upc);
console.log(array[i].itemcode);
console.log(array[i].modelname);
console.log(array[i].quantity);
if(jsonp.data=="noresult")
{
$('#boxmsg').show();
}
else{
var table = '<tr class="active" style="height:40px;font-weight:bold">';
table+= '<td><input type="checkbox" checked disabled /></td>'
table += '<td id='+k+'>'+k+'</td>';
table += '<td id='+array[i].documentno+'>'+array[i].documentno+'</td>';
table += '<td id='+array[i].upc+'>'+array[i].upc+'</td>';
table += '<td id='+array[i].itemcode+'>'+array[i].itemcode+'</td>';
table += '<td> '+array[i].modelname+'</td>';
table += '<td>' + array[i].quantity + '</td>';
table += '<td id="my'+k+array[i].itemcode+'" class="editable">' + array[i].quantity + '</td>';
table += '</tr>';
$row = table;
k=k-1;
$("#itemtab").prepend($row);
}
}
},
error : function() {
alert("Error IN UP")
}
});
}
else
{
console.log("Search in Localdb");
$.ajax({
type : 'POST',
url : '@routes.Application.getitemsfromlocal()',
data : {
boxnum:box
},
success : function(data) {
$("#main").slideUp();
$("#itemscanning").slideDown();
$("#itemtab").empty();
$("#boxnum").text(box)
$.each(data, function(m, item) {
var table = '<tr class="active" style="height:40px;font-weight:bold">';
table+= '<td><input type="checkbox" checked disabled/></td>'
table += '<td id='+item[0]+'>'+item[0].trim()+'</td>';
table += '<td id='+item[1]+'>'+item[1].trim()+'</td>';
table += '<td id='+item[2]+'>'+item[2].trim()+'</td>';
table += '<td id='+item[3]+'>'+item[3].trim()+'</td>';
table += '<td> '+item[4]+'</td>';
table += '<td>' + item[5] + '</td>';
table += '<td id="myid'+k+item[3].trim()+'" class="editable">' + item[6] + '</td>';
table += '</tr>';
$row = table;
$("#itemtab").prepend($row);
});
},
error : function() {
console.log("Error in Searching Local DB");
}
});
}
},
error : function() {
console.log("errr");
}
});
$("#itemtab").on('click','.editable',function(e){ //assign event to editableTD class
var i=0;
var id=$(this).attr('id');
e.stopPropagation(); //<-------stop the bubbling of the event here
var value = $('#'+id).html();
console.log(id+i);
updateVal('#'+id, value);
});
function updateVal(currentEle, value) {
console.log("Current Element is"+currentEle);
$(currentEle).html('<input class="thVal" maxlength="4" type="text" width="2" value="0" />');
$(".thVal").focus();
$(".thVal").keyup(function (event) {
if (event.keyCode == 13) {
$(currentEle).html($(".thVal").val().trim());
//$(currentEle).html($(".thVal").val().trim());
}
});
$(document).click(function () { // you can use $('html')
$(currentEle).html($(".thVal").val().trim());
});
}
以上代码正在正确更新。但问题是它在所有行中的更新值。例如,一列有3个值,即10,20,30。如果我更新10到12,它将在所有列中更新。请仔细研究并提供解决方案