使用javascript / jquery为checkboxlist添加值 [英] add value to checkboxlist using javascript/jquery

查看:105
本文介绍了使用javascript / jquery为checkboxlist添加值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用javascript /jquery为checkboxlist添加一个值。下面的代码是我的示例代码

I want to add a value to a checkboxlist using javascript /jquery.The code below is my sample code

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

                                           }

                                       }

                                   });

                               }


推荐答案

来源:

http:// forums .asp.net / p / 1416683 / 3127300.aspx

CheckBoxList(或RadioButtonList)用CheckBoxes和HTML标签呈现为标签标签中的元素。要添加项目,您需要添加一个和/或表格,我绝对不建议您使用JavaScript,因为它们不会在服务器端持久存在,如果发生PostBack则会消失。我建议你做一个PostBack并在服务器端添加项目。

A CheckBoxList (or RadioButtonList for that matter) renders as a tag with CheckBoxes and an HTML Label element in the tags. To add items you would need to add a and or to the table, which I definitely would not advise you to do with JavaScript, since they would not persist server-side and would disappear if a PostBack occurred. I'd suggest you do a PostBack and add the items server-side.

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

这篇关于使用javascript / jquery为checkboxlist添加值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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