使用javascript/jquery向checkboxlist添加值

使用javascript/jquery向checkboxlist添加值,javascript,jquery,Javascript,Jquery,我想使用javascript/jquery向复选框列表添加一个值 function getExpertise() { $.ajax({ type: "POST", url: "Sample.asmx/GetExpertiseBySpecialization", data: "{sId: '" + $('#<%=ddlSpecialization.ClientID%>').val() + "'

我想使用javascript/jquery向复选框列表添加一个值

function getExpertise() {

          $.ajax({

           type: "POST",

           url: "Sample.asmx/GetExpertiseBySpecialization",

           data: "{sId: '" + $('#<%=ddlSpecialization.ClientID%>').val() + "'}",

                 contentType: "application/json; charset=utf-8",

                 dataType: "json",

                 success: function(response) {

         var expertise = (typeof response.d) == 'string' ? eval('(' + response.d + ')') : response.d;

        $('#<%=chkExpertise.ClientID%>').attr('disabled', false).removeOption(/./).addOption('-1', 'Please select expertise');


        for (var i = 0; i < expertise.length; i++) {

            var val = expertise[i].Id;

            var text = expertise[i].Expertise;

            $('#<%=chkExpertise.ClientID%>').addOption(val, text, false);

                                           }

                                       }

                                   });

                               }
函数getexperties(){
$.ajax({
类型:“POST”,
url:“Sample.asmx/GetExpertiseBySpecialization”,
数据:“{sId:'”+$('#').val()+“}”,
contentType:“应用程序/json;字符集=utf-8”,
数据类型:“json”,
成功:功能(响应){
var experties=(typeof response.d)='string'?eval('('+response.d+')):response.d;
$('#').attr('disabled',false).removeOption(/./).addOption('-1','请选择专业技能');
对于(var i=0;i
来源:

复选框列表(或RadioButtonList)呈现为标记,标记中包含复选框和HTML标签元素。要添加项目,您需要在表中添加and或,我绝对不建议您使用JavaScript,因为它们不会在服务器端持久化,并且在发生回发时会消失。我建议您进行回发,并将项目添加到服务器端

<asp:CheckBoxList id="CheckBoxList1" runat="server">
 <asp:listitem Value="1">Item 1</asp:listitem>
</asp:CheckBoxList>
<input type="button" onclick="addToCheckBoxListControl('Item 2', '2');" value="Add To CheckBoxList" />

<script type="text/javascript">
<!--
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);
}
// -->
</script>

项目1

变量计数=2;//假设复选框列表最初有1个元素(元素1)
函数addElement(){
var tableRef=document.getElementById(“”);
var tableRow=tableRef.insertRow();
var tableCell=tableRow.insertCell();
var checkBoxRef=document.createElement('input');
var labelRef=document.createElement('label');
checkBoxRef.type='checkbox';
labelRef.innerHTML='Element'+count;
checkBoxRef.value=计数;
计数++;
tableCell.appendChild(checkBoxRef);
tableCell.appendChild(labelRef);
}

你是指ASP.NET CheckBoxList控件吗?你能发布到目前为止的HTML和Javascript吗?是的,它是ASP.NET CheckBoxList控件。你看到这篇文章了吗?希望能对某人有所帮助。此代码有效,问题是,它应该包含一个默认项(如第1项)。现在我如何使用javascript删除这个默认数据。如果我已经使用javascript添加了一个项目,我想删除它
<asp:CheckBoxList id="CheckBoxList1" runat="server">
 <asp:listitem Value="1">Item 1</asp:listitem>
</asp:CheckBoxList>
<input type="button" onclick="addToCheckBoxListControl('Item 2', '2');" value="Add To CheckBoxList" />

<script type="text/javascript">
<!--
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);
}
// -->
</script>
 <script type="text/javascript">
            var count = 2; // it is assumed the CheckBoxList initially has 1 element (Element 1)
            function addElement() {
                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 = 'Element '+count;
                checkBoxRef.value = count;

                count++;

                tableCell.appendChild(checkBoxRef);
                tableCell.appendChild(labelRef);
            }
        </script>