65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 JavaScript 在表中添加和删除多个选定行

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.79/5 (8投票s)

2005年3月6日

viewsIcon

87575

downloadIcon

895

本文将帮助用户在 ASP.NET 中向表格添加行,并使用 JavaScript 从表格中删除多个选定行。

用法

在网页上放置一个隐藏控件(“hdhidden”),并将其默认值设置为 1,以及 runat = server。当在表格中添加新行时,其值会加 1,但删除行时不会改变。以下是执行表格中行添加和删除的两个函数。

<script language="javascript"> 
function deleteTableRows() 
{
    var counts=parseInt(document.all("hdhidden").value);
    //variable counts will contains the value of hidden control 
    //("hdhidden")
    var i;
    var a=0;
    for(i=1;i<counts;i++)
    {
        itemCheck="chk_"+i; 
        //itemCheck contians the checkboxes id. 
        if(document.all[itemCheck]!=undefined) 
        { 
            //if itemCheck is not undefined then adding 1 in "a" 
            //value
            a = a + 1; 
            //check whether is checkbox is checked or not
            if(document.all[itemCheck].checked==true) 
            { 
                //if chekbox is checked then compare 
                //"a" with "i"
                if(a!=i) 
                {
                    //if "a" is not equals to i then delete row 
                    //from the table on the basis of "a" value.
                    tblvalue.deleteRow(a); 
                } 
                else 
                {
                    //else delete row from the table on the 
                    //basis of "i" value.
                    tblvalue.deleteRow(i); 
                } 
                //less 1 from "i" and "a" values.
                i = parseInt(i) - 1;
                a = a - 1; 
            }
        }
    }
    return false; 
}

function AddItem() 
{ 
    var counts=parseInt(document.all("hdhidden").value); 
    //varibale counts will contains the value 
    //of hidden control (hdhidden)
    var Row,Cell;
    var CurrentPosition=parseInt(counts);
    //asigned value of variable "counts" to varibale "CurrentPosition" 
    Row = window.tblvalue.insertRow();
    Cell = Row.insertCell(); 
    //insert a cell in a row;
    //insert a checkbox in a cell with the name 
    //"chk_" as prefix + its current positon 
    //so thant checkbox has unique id
    Cell.innerHTML="<input type=checkbox name=chk_" + 
       CurrentPosition +" id=chkbox_"+ CurrentPosition+"; value='C'>"; 
    Cell = Row.insertCell(); 
    //insert another cell to row
    //insert a textbox in a cell with the name 
    //"txtval1_" as prefix + its current position
    Cell.innerHTML="<input style=Font-size=11; type=Text name=txtval1_" 
                    + CurrentPosition +" id=txtval1_"+ CurrentPosition+>"; 
    //insert another cell in a row
    Cell = Row.insertCell(); 
    //insert a textbox in a cell with the name 
    //"txtval2_" as prefix + its current position
    Cell.innerHTML="<input style=Font-size=11; type=Text name=txtval2_" + 
                        CurrentPosition +" id=txtval2_"+ CurrentPosition+>"; 
    //update the value of hidden control (hdhidden) by adding 1 
    //to its previous value
    document.all("hdhidden").value = parseInt(counts) + 1; 
    return false; 
} 
</script>
© . All rights reserved.