如何在jquery中绑定checkboxlist [英] How to bind checkboxlist in jquery
本文介绍了如何在jquery中绑定checkboxlist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想通过jquery绑定checkboxlist
我的代码是
< asp:CheckBoxList ID < span class =code-keyword> = chkListMY runat = server RepeatColumns = 1 >
< asp :ListItem 文本 = 值 = > ; < / asp:ListItem > ;
< / asp:CheckBoxList >
成功:函数(数据)
{if(data.length){
var repeatColumns = parseInt(<% = chkListMY.RepeatColumns%> 中);
var cell = $([id * = chkListMY] td)。eq(0).clone(true);
$([id * = chkListMY] tr)。remove();
for(var i = 0; i < data.length; i ++)
{
var row < span class =code-attribute> = $( < tr / > 跨度>);
$([id * = chkListMY] tbody)。append(row);
var checkbox = $(input [type = checkbox],cell); //为每个CheckBox设置唯一ID。
复选框[0] .id =复选框[0] .id.replace(0,i); //为每个CheckBox提供文本。
复选框[0] .text = data [i] .Text
//设置CheckBox值。 checkbox.val(数据[I]。价值);
var label = cell.find(label);
if(label.length == 0){
label = $(< label / > );
}
//设置Label的'for'属性。
label.attr(for,checkbox [i] .id);
//在Label中设置文本。
label.html(data [i] .Text);
//将标签附加到单元格。
cell.append(label);
//将单元格附加到表格行。
row.append(cell);
cell = $([id * = chkListMY] td)。eq(0).clone(true);
};
}
}
< script type = text / javascript>
$( document )。ready( function (){
getMonthYearCheckIndexChange();
< / script>
function getMonthYearCheckIndexChange(){
$( [id * = chkListMY]输入[type = checkbox])。点击( function (){
var chks = $( [id * = chkListMY] input [type =复选框]);
var str = ;
for ( var i = < span class =code-digit> 0 键;我< chks.length; i ++){
if (chks [i] .checked){
str = str + chks [i] .text + ' ,';
}
}
str = str.substr( 0 ,str.length - 1 跨度>);
alert(str);
});
}
它最初在复选框列表中显示空复选框但是我的要求是,它(空复选框)不应该显示。
当我选中任何复选框时, 工作意味着调用给定函数
解决方案
([id * = chkListMY] td)。eq(0).clone(true);
< blockquote>([id * = chkListMY] tr)。remove();
for(var i = 0; i < data.length; i ++)
{
var row < span class =code-attribute> =
(
< tr / > );
I want to bind checkboxlist by jquery
my code is
<asp:CheckBoxList ID="chkListMY" runat="server" RepeatColumns="1">
<asp:ListItem Text="" Value=""></asp:ListItem>
</asp:CheckBoxList>
success: function (data)
{if (data.length){
var repeatColumns = parseInt("<%=chkListMY.RepeatColumns%>");
var cell = $("[id*=chkListMY] td").eq(0).clone(true);
$("[id*=chkListMY] tr").remove();
for (var i = 0; i < data.length; i++)
{
var row = $("<tr />");
$("[id*=chkListMY] tbody").append(row);
var checkbox = $("input[type=checkbox]", cell); //Set Unique Id to each CheckBox.
checkbox[0].id = checkbox[0].id.replace("0", i); //Give text to each CheckBox.
checkbox[0].text = data[i].Text
//Set the CheckBox value. checkbox.val(data[i].Value);
var label = cell.find("label");
if (label.length == 0) {
label = $("<label />");
}
//Set the 'for' attribute of Label.
label.attr("for", checkbox[i].id);
//Set the text in Label.
label.html(data[i].Text);
//Append the Label to the cell.
cell.append(label);
//Append the cell to the Table Row.
row.append(cell);
cell = $("[id*=chkListMY] td").eq(0).clone(true);
};
}
}
<script type="text/javascript">
$(document).ready(function () {
getMonthYearCheckIndexChange();
</script>
function getMonthYearCheckIndexChange() {
$("[id*=chkListMY] input[type=checkbox]").click(function () {
var chks = $("[id*=chkListMY] input[type=checkbox]");
var str = "";
for (var i = 0; i < chks.length; i++) {
if (chks[i].checked) {
str = str + chks[i].text + ',';
}
}
str = str.substr(0, str.length - 1);
alert(str);
});
}
it initially shows empty check box in checkboxlist but my requirement is ,it(empty check box) should not be show.
And When I checked any checkbox it is working means calling given function
解决方案
("[id*=chkListMY] td").eq(0).clone(true);
("[id*=chkListMY] tr").remove(); for (var i = 0; i < data.length; i++) { var row =
("<tr />");
这篇关于如何在jquery中绑定checkboxlist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文