2013-04-05 81 views
0
<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启用的CheckBoxList在IE中,asp.net,jQuery的

它在IE8不wroking,这里是下面的代码。 它检查所有复选框,但保持它们禁用,请解决任何问题吗?

+1

呈现页面的来源是什么样子?具体来说,复选框的列表? – tymeJV 2013-04-05 18:12:28

+0

<表ID = “ctl00_cphMain_chkList” 禁用= “禁用” BORDER = “0”> ​​ <跨度禁用= “禁用”> <输入的ID = “ctl00_cphMain_chkList_0” 类型= “复选框” 名称=“ctl00 $ cphMain $ chkList $ 0" 禁用= “禁用”/> <标签= “ctl00_cphMain_chkList_0”> 1 \t \t \t​​<跨度禁用= “禁用”><输入的ID = “ctl00_cphMain_chkList_1” type =“checkbox”name =“ctl00 $ cphMain $ chkList $ 1”禁用= “禁用”/><标签= “ctl00_cphMain_chkList_1”> 2 \t \t \t – Avinash 2013-04-05 18:21:12

回答

1

尝试使用prop属性,而不是.checked

$("#btn").on("click",function(){ 
    $('input:checkbox').each(function(){ 
    $(this).prop("checked", "true"); 
    }); 
}); 

的jsfiddlehttp://jsfiddle.net/hxS7M/1/

编辑:这也将让他们启用。

$("#btn").on("click",function(){ 
    $('input:checkbox').each(function(){ 
    $(this).prop("checked", "true").prop("disabled",false); 
    }); 
}); 

的jsfiddlehttp://jsfiddle.net/hxS7M/4/

+0

对不起,请再见到我的问题: 它检查所有的复选框,但仍会保留禁用,任何解决方案吗? – Avinash 2013-04-05 18:30:48

0

试试这个:

$(document).ready(function() { 
    $('#btnFoo').click(function() { 
    $('#chkList input:checkbox').each(function(i,e) { 
     $(e).attr("checked", true);      
    }) 
    }); 
}) 

+0

应该将'attr()'改为'prop()' – tymeJV 2013-04-05 18:30:44

+0

请检查我的问题: 它检查所有复选框,但保持它们禁用,请解决任何问题? – Avinash 2013-04-05 18:31:21

0

要启用它们,这样做

$('#<%= chkList.ClientID%>input:checkbox').prop('disabled', false); 
1

每个人的脚本都在正常的HTML页面上工作。但是,您正在使用按钮控件,并且它将回发到服务器,除非您明确地取消该事件。

checkAll返回false onclick事件,以便它不会回发到服务器。

enter image description here

<%@ 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。

+0

+1我忽略了提交按钮。非常好。 – 2013-04-05 21:28:40