gpt4 book ai didi

java - 获取html中动态表的行id

转载 作者:行者123 更新时间:2023-12-03 09:58:36 24 4
gpt4 key购买 nike

以下函数用于创建包含数据的动态表并执行点击操作。当用户单击一行时,我们需要获取该特定的行 ID。但我总是只得到最后一行 id。我尝试了很多方法。请简化一下:

function loader() {

//alert("alert is here");
//calling java function
var jsonData = window.webConnector.load();
//toaster.print(jsonData);
//parsing json data
var data=JSON.parse(jsonData);
//document.getElementById("test").innerHTML = data.pos[0].store_name;
var totalBillAmount=0.0;
var posid;
var row=[];
//creating dynamic table
for(var i=0;i<data.pos.length;i++){

posid=data.pos[i].posid;
var radioHtml = '<input type="radio" name="' + posid + '" id="radio" />';


var table = document.getElementById("mytbody");
row[i] = table.insertRow(i);

var cell1 = row[i].insertCell(0);
var cell2 = row[i].insertCell(1);
var cell3 = row[i].insertCell(2);
var cell4 = row[i].insertCell(3);

//cell1.innerHTML = radioHtml;
cell1.innerHTML = radioHtml+" "+data.pos[i].date_time;
cell2.innerHTML = data.pos[i].store_name;
cell3.innerHTML = data.pos[i].billamount;
cell4.innerHTML = posid;
totalBillAmount=totalBillAmount+parseFloat(data.pos[i].billamount);
//performing operations when row is cliked
row[i].onclick=function(){
alert(posid);//it gives only last row posid
//var rowIndex=this.parentNode.rowIndex;
var rowIndex=$(this).parent().find(row);
var row1=rowIndex.eq(i).text();
radioHtml.setChecked=true;
alert(this.parentElement.rowIndex);
//var rowindex=$(this).closest(row).index();
//above line gives 1 and -1
var rowindex=table.parentNode.rowIndex;
alert(rowindex) //it gives undefined

}
}

最佳答案

点击是 future ,您必须使用正确的闭包设置(IIFE/绑定(bind)等)来保留 transient 范围值。

row[i].onclick = (function (index) { //capture more aguments as passed
return function () {
alert(index);
//more code
}
}(i)); //pass in more arguments as needed

关于java - 获取html中动态表的行id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30663915/

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