Gridview 中的radio唯一選中

iDotNetSpace發表於2008-09-27

 

 

方法1:如果你只是想唯一選中,不觸發任何事件:你可以用前臺控制元件

方法2:Gridview中的radio用伺服器控制元件

 

後臺:this._offerCounties.Attributes.Add("onclick", "postBackByObject()");

前臺程式碼:


        function postBackByObject()
        {
            var element = window.event.srcElement;
            if (element.tagName == "INPUT" && element.type == "checkbox")
            {
                var checkedState = element.checked;
                while (element.tagName != "TABLE")
                element = element.parentElement;

                UnCheck(element);
                element = element.nextSibling;
                if (element == null)
                return;

                var childTables = element.getElementsByTagName("TABLE");
                for (var tableIndex = 0; tableIndex < childTables.length; tableIndex++)
                Check(childTables[tableIndex], checkedState);
            }
        }

        function UnCheck(table)
        {
            if (table == null || table.rows[0].cells.length == 2) // This is the root
                return;
            var parentTable = table.parentElement.previousSibling; 
            Check(parentTable, false);
            UnCheck(parentTable);
        }

        function Check(table, checked)
        {
            var checkboxIndex = table.rows[0].cells.length - 1;    
            var cell = table.rows[0].cells[checkboxIndex];
            var checkboxes = cell.getElementsByTagName("INPUT");
            if (checkboxes.length == 1)
                checkboxes[0].checked = checked;
        }

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-465886/,如需轉載,請註明出處,否則將追究法律責任。

相關文章