2011-04-13 59 views
1

通過javascript啓用和禁用複選框,例如,當我檢查chk1,然後chk2,chk3和chk4將被啓用,並且如果我取消選中chk1,則chk2,chk3和chk4將通過javascript被禁用?通過javascript啓用/禁用asp.net複選框

這裏是我的代碼:

<script type="text/javascript"> 
$('chkTAR').clicked(function(){ 
     { 
     if($('chkTAR :checked').length == 2) 

     $('chkOasis, chkPOT').disabled = true; 
     else 
     $('chkOasis, chkPOT').disabled = false 
     }); 
</script> 
+0

這裏沒有問題。 – 2011-04-13 05:57:07

+0

檢查我的解決方案 – 2011-04-13 06:24:52

回答

6

你可以試試這個:

<script language="javascript" type="text/javascript"> 

     function oncheckchange(checked) { 
      var chk2 = document.getElementById('chk2'); 
      chk2.disabled = checked; 
     } 
    </script> 

    <asp:CheckBox OnClick="oncheckchange(this.checked);" ID="chk1" runat="server" /> 
    <asp:CheckBox ID="chk2" runat="server" ClientIDMode="Static" /> 

注意CHK2的的ClientIDMode被設置爲 「靜態」。

+0

它的工作正常..謝謝!!無論如何, – 2011-04-13 06:23:37

+0

,爲什麼我應該把clientidode變成靜態? – 2011-04-13 06:24:59

0
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication3._Default" %> 

<!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="Scripts/jquery-1.4.1.js" type="text/javascript"></script> 
    <script type="text/javascript"> 
     $().ready(function() { 
      $('#checkbox1, #checkbox2').click(function() { 
       if ($('#checkbox1:checked,#checkbox2:checked').length == 2) 
        $('#checkbox3, #checkbox4').attr('disabled','true'); 
       else 
        $('#checkbox3, #checkbox4').attr('disabled',''); 
      }); 
     }); 
    </script> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
     <input type="checkbox" id="checkbox1" value="c1" /> 
     <input type="checkbox" id="checkbox2" value="c2" /> 
     <input type="checkbox" id="checkbox3" value="c3" /> 
     <input type="checkbox" id="checkbox4" value="c4" /> 
    </div> 
    </form> 
</body> 
</html> 
+0

也許它需要一些調整,但你可以使它工作 – 2011-04-13 05:57:07

+0

我應該把這裏面的ASP或我會創建一個JS? – 2011-04-13 05:57:45

+0

你,應該在aspx頁面裏面寫腳本。 – 2011-04-13 05:58:55

0
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default6.aspx.cs" Inherits="Default6" %> 

<!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 id="Head1" runat="server"> 
    <title></title> 

    <script src="http://code.jquery.com/jquery-1.5.js"></script> 

    <script language="javascript"> 

     function EnableDisableDIV() { 

      if ($("#chkShowPersonal").attr("checked")) { 

       $("#dvPersonal").show(); 

      } else { 

       $("#dvPersonal").hide(); 

      } 

     }   

    </script> 

</head> 
<body> 
    <form id="form1" runat="server"> 
    <asp:CheckBox ID="chkShowPersonal" onclick="EnableDisableDIV()" runat="server" Text="Fruits" /> 
    <div id="dvPersonal" style="display: none"> 
     <asp:CheckBox ID="chk1" runat="server" Text="Apple" /> 
     <asp:CheckBox ID="chk2" runat="server" Text="Banana" /> 
     <asp:CheckBox ID="chk3" runat="server" Text="Orange" /> 
    </div> 
    </form> 
</body> 
</html>