开发者

accessing checkboxlist text through javascript

I have a checkbox list i have which i have bou开发者_开发问答nd some document name to it's text field.now i need to find only the checkboxes which the text contains the .pdf extension and check them all in a single checkbox click.i have written the following javascript but it doesn't work for me

function CheckAllPDF() {
            var checkBoxList = document.getElementById("<%= cblFiles.ClientID %>");
            var checkBoxes = checkBoxList.getElementsByTagName("input");

                for (i = 0; i < checkBoxes.length; i++) {
                    var string = checkBoxes[i].parentNode.getElementsByTagName('label').innerHTML;
                    var match = string.indexOf(".pdf");

                    if (match != -1) {
                        checkBoxes[i].checked = true;
                    }
                    else {
                        checkBoxes[i].checked = false;
                    }
                } 

can some one help?


When you put an asp.net check box list in the page it is translated to a list of input of type checkbox so you need to access each control and check it so you javascript code should look like:

        //Get the main id of the asp.net check box list
        var checkboxId = '<%=  CheckBoxList1.ClientID %>';

        //Loop on all generated input check boxes where the count function determine the number of generated checkboxes
        for (var i = 0; i < <%=  Count() %>; i++) {
        //Append the count on the main asp.net check box id the value ('_'+i)
            var checkBox = document.getElementById(checkboxId + '_' + i);
            var checkBoxValue = checkBox.value;

            var match = checkBoxValue.indexOf(".pdf");
            if (match != -1) {
                checkBox.checked = true;
            }
            else {
                checkBox.checked = false;
            }
        }

And in your code behind write the count function as follows:

public int Count()
    {
        return CheckBoxList1.Items.Count;
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜