簡體   English   中英

在HTML中獲取動態表的行ID

[英]Get row id of dynamic table in html

以下功能用於創建帶有數據的動態表並執行單擊操作。 當用戶被單擊時,我們需要獲取該特定的行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

        }
}

點擊是期貨,您必須使用正確的閉包設置(IIFE / bind等)來保留臨時范圍值。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM