我想使用javascript /jquery为checkboxlist添加一个值。下面的代码是我的示例代码
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);
}
}
});
}
答案 0 :(得分:0)
$('#checkbox_id').attr('checked', true)
答案 1 :(得分:0)
来源:
http://forums.asp.net/p/1416683/3127300.aspx
CheckBoxList(或RadioButtonList)呈现为带有CheckBoxes的标签和标签中的HTML Label元素。要添加项目,您需要添加一个和/或表格,我绝对不建议您使用JavaScript,因为它们不会在服务器端持久存在,如果发生PostBack则会消失。我建议你做一个PostBack并添加服务器端的项目。
<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>
答案 2 :(得分:0)
<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>