我有asp:CheckBoxList
从数据库加载项目。
我想写一个不显示的脚本。检查每个支票上的复选框 我的aspx页面如下:
<div id="rfd-demo-zone2" class="placeholderContent accordionEmpGroup-child-subGroup-container">
<asp:CheckBoxList ID="ckbList2DesignationUc" runat="Server" RepeatLayout="Flow" RepeatDirection="Horizontal" Width="100%" ></asp:CheckBoxList>
</div>
脚本:
<script type="text/javascript">
$('#ckbList2DesignationUc input:checked').each(function () {
alert('hi');
});
</script>
在上面的脚本中,我试图获得警报,但它没有显示
答案 0 :(得分:0)
将脚本放在document.ready中以确保脚本中html元素的可用性,并使用attribute contains selector查找复选框列表的复选框。
<script type="text/javascript">
$(document).ready(function(){
$('[id*=ckbList2DesignationUc]:checked').each(function () {
alert('hi');
});
});
</script>
在更改复选框时触发事件
<script type="text/javascript">
$(document).ready(function(){
$('[id*=ckbList2DesignationUc]').change(function () {
alert(this.id);
});
});
</script>
答案 1 :(得分:0)
可能的解决方案:
<div id="rfd-demo-zone2" class="placeholderContent accordionEmpGroup-child-subGroup-container">
<asp:CheckBoxList ID="ckbList2DesignationUc" runat="Server" RepeatLayout="Flow" RepeatDirection="Horizontal" Width="100%" >
</asp:CheckBoxList>
</div>
并访问它:
$("#<%=ckbList2DesignationUc.ClientID %> input[type=checkbox]:checked").each(function() {
alert('checked');
});
答案 2 :(得分:0)
尝试以下代码:
<强>解决方案-1 强> 每当您选中复选框时,警报将显示复选框中已选项的值。
Javascript代码:
&LT; script type =“text / javascript”&gt;
function itemClick(radioButton) {
var Control;
var selectedItems;
selectedItems = '';
Control = document.getElementById("<%=ckbList2DesignationUc.ClientID %>").getElementsByTagName("input");
for (var i = 0; i < Control.length; i++) {
if (Control[i].checked == true)
selectedItems += i.toString()+',';
}
alert(selectedItems);
}
</script>
ASPX代码:
protected void Page_Load(object sender, EventArgs e)
{
for (int index = 0; index < 10; index++)
{
ckbList2DesignationUc.Items.Add(new ListItem("Item" + index, index.ToString()));
}
foreach (ListItem Li in ckbList2DesignationUc.Items)
{
Li.Attributes.Add("onclick", "return itemClick()");
}
}
使用Jquery所需的解决方案:
解决方案-2
< script type="text/javascript">
$(document).ready(function () {
var selectedCheckBoxes = '';
$('#ckbList2DesignationUc').each(function () {
var items = new Array();
$(this).find("input:checkbox").each(function () {
$(this).click(function () {
selectedCheckBoxes += $(this).val() + ",";
alert(selectedCheckBoxes);
});
});
});
});
< /script>
< asp:CheckBoxList ID="ckbList2DesignationUc" runat="Server" RepeatLayout="Flow" RepeatDirection="Horizontal" Width="100%">
< /asp:CheckBoxList>
答案 3 :(得分:0)
客户端:
$('.CheckBoxList').each(function () {
var items = new Array();
$(this).find("input:checkbox").each(function () {
if ($(this).attr("checked") == true) {
items[items.length] = $(this).next().html();
}
});
$(this).parent().find('.txtList').val(items.toString());
});
Html MarkUp:
<asp:TextBox runat="server" ID="txtabc" CssClass="txtList txtformatcss" Width="160px" ViewStateMode="Disabled"></asp:TextBox>
<asp:CheckBoxList ID="chkabc" runat="server"
RepeatLayout="Flow" CssClass="CheckBoxList">
</asp:CheckBoxList>
此处CheckBoxList
是在checkboxlist control
答案 4 :(得分:0)
这可能会对您有所帮助:您可以在代码隐藏文件以及脚本代码enable AutoPostBack property of checkboxlist to true
protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{
int i=0;
foreach (ListItem li in CheckBoxList1.Items)
{
if (li.Selected)
i++;
}
Response.Write(i);
}