通过javascript访问复选框列表文本

发布于 2024-11-25 01:19:40 字数 795 浏览 5 评论 0原文

我有一个复选框列表,我已将一些文档名称绑定到它的文本字段。现在我只需要找到文本包含 .pdf 扩展名的复选框,然后在单个复选框中单击将它们全部选中。我编写了以下内容javascript 但它对我不起作用

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;
                    }
                } 

有人可以帮忙吗?

I have a checkbox list i have which i have bound 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

娇柔作态 2024-12-02 01:19:40

当您将 asp.net 复选框列表放入页面中时,它会转换为复选框类型的输入列表,因此您需要访问每个控件并检查它,以便您的 javascript 代码应如下所示:

        //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;
            }
        }

在后面的代码中写入计数函数如下:

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

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;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文