<asp:CheckBoxList ID="chkList" runat="server" Enabled="false" >
<asp:ListItem Text="1" Value="1"></asp:ListItem>
<asp:ListItem Text="2" Value="2"></asp:ListItem>
</asp:CheckBoxList>
<asp:Button ID="btnFoo" runat="server" Text="Test" />
<script type="text/javascript" language="javascript">
$(document).ready(function () {
$('#<% = btnFoo.ClientID %>').click(function () {
//var targetValue = 2;
var items = $('#<% = chkList.ClientID %> input:checkbox');
for (var i = 0; i < items.length; i++) {
//alert(i);
//if (items[i].value == targetValue) {
items[i].checked = true;
// break;
//}
}
$('#<%= chkList.ClientID%>input:checkbox').removeAttr('disabled'); return false;
})
});
</script>
注意:它适用于Chrome,FF不适用于IE
它在IE8中没有变化,这是下面的代码。 它检查所有复选框,但保持禁用,任何解决方案好吗?
答案 0 :(得分:1)
尝试使用prop
属性而不是.checked
:
$("#btn").on("click",function(){
$('input:checkbox').each(function(){
$(this).prop("checked", "true");
});
});
jsFiddle :http://jsfiddle.net/hxS7M/1/
修改:这也会启用它们。
$("#btn").on("click",function(){
$('input:checkbox').each(function(){
$(this).prop("checked", "true").prop("disabled",false);
});
});
jsFiddle :http://jsfiddle.net/hxS7M/4/
答案 1 :(得分:1)
每个人的脚本都适用于普通的HTML页面。但是,您使用按钮控件并发回服务器,除非您明确取消该活动。
checkAll
向onclick事件返回false,因此它不会回发给服务器。
<%@ Page Language="C#" AutoEventWireup="true"
CodeBehind="WebForm7.aspx.cs" Inherits="WebApplication2010.WebForm7" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:CheckBoxList ID="chkList" runat="server" Enabled="False">
<asp:ListItem Text="1" Value="1"></asp:ListItem>
<asp:ListItem Text="2" Value="2"></asp:ListItem>
</asp:CheckBoxList>
<asp:Button ID="btnFoo" runat="server" Text="Test"
OnClientClick="return checkAll();" />
<script type="text/javascript" language="javascript">
function checkAll() {
$('#<% = chkList.ClientID %> :checkbox').each(function () {
$(this).prop("checked", "true").prop("disabled",false);
});
return false;
}
</script>
</div>
</form>
</body>
</html>
归功于@HanletEscaño。
答案 2 :(得分:0)
试试这个:
的
的$(document).ready(function () {
$('#btnFoo').click(function () {
$('#chkList input:checkbox').each(function(i,e) {
$(e).attr("checked", true);
})
});
})
的
答案 3 :(得分:0)
要启用它们,请执行此操作
$('#<%= chkList.ClientID%>input:checkbox').prop('disabled', false);