如何在JavaScript中将列表项添加到复选框列表 [英] How to add list ittems to checkboxlist in javascript

查看:100
本文介绍了如何在JavaScript中将列表项添加到复选框列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有键值对的数组,我必须遍历该数组,并使用javascript ...将列表项添加到复选框中.

我该如何实现...?

任何建议表示赞赏.


我的代码:

I have a array with key value pair, I have to iterate through the array and add list items to check list box using javascript...

How can I achieve this...?

any suggestion is appreciated.


My Code:

var tableRef = document.getElementById("ContentPlaceHolder1_chklstProvider");
        var checkBoxArray = tableRef.getgetElementsByTagName('input');
        for (var rowIndex = 1; rowIndex < arr.length; rowIndex++)
        {
          var tmparr = arr[rowIndex - 1].split('^');
//            var lastrow = tableRef.rows.length;
//            var tableRow = tableRef.insertRow(1);
//            var tableCell = tableRow.insertCell(0);
//            var checkBoxRef = document.createElement('input');
//            var labelRef = document.createElement('label');
//            checkBoxRef.type = 'checkbox';

//            textValue = lastrow + 1;
//            valueValue = lastrow + 1;
//            labelRef.innerHTML = tmparr[1];
//            checkBoxRef.value = tmparr[0];
//            tableCell.appendChild(checkBoxRef);
            //            tableCell.appendChild(labelRef);
            var option = document.createElement('input');
            var label = document.createElement('label');


            option.type = 'checkbox';
            label.innerHTML = tmparr[1];
            option.value = tmparr[0];


            document.getElementById("ContentPlaceHolder1_chklstProvider").appendChild(option);
            document.getElementById("ContentPlaceHolder1_chklstProvider").appendChild(label);




注释代码是第一种方法,另一种是另一种方法..

我首先没有获得复选框列表ID,
它为null或未定义....


请帮助我




commented code is 1st approach, the other is another approach..

I failed to get the checkbox list Id first of all,
It''s been null or undefined....


please help me

推荐答案

^ ]
Source[^]
function addToCheckBoxListControl(textValue, valueValue)
{
 var tableRef = document.getElementById('<%= CheckBoxList1.ClientID %>');

 var tableRow = tableRef.insertRow();
 var tableCell = tableRow.insertCell();

 var checkBoxRef = document.createElement('input');
 var labelRef = document.createElement('label');

 checkBoxRef.type = 'checkbox';
 labelRef.innerHTML = textValue;
 checkBoxRef.value = valueValue;

 tableCell.appendChild(checkBoxRef);
 tableCell.appendChild(labelRef);
}


这篇关于如何在JavaScript中将列表项添加到复选框列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆