2014-02-22 53 views
0

我對asp和java非常陌生: 我設計了一個頁面,裏面有一個表格和一些複選框,我希望使用腳本設置一個條件提醒用戶不要檢查四個以上的複選框。或者,如果用戶選中了四個複選框,則此表中可用的其他剩餘複選框必須被禁用,如果用戶取消選中任何選中的複選框,則所有禁用的複選框都應該啓用以供新選擇。有一些複選框屬於同一組(單元格),如果用戶點擊其中一個複選框,該組(單元格)中的其他選定複選框必須取消選中,就像在toggle case中一樣,只有一個checkbiox必須在該單元格中選中。在java中完成,但不知道如何檢查超過四個複選框。 (P.S)在表中複選框GE是cheked和殘疾人此複選框(I組)是4個複選框,它應該是強制性必檢計數asp.net表中的複選框使用java腳本並禁用其他人

<script type="text/javascript"> 
    function ChkValidate() { 
     var counter = 0; 


     var panel = document.getElementById ("panel1") 
     for (i = 0; i < panel.children.length; i++) { 
      if (panel.children[i].type == 'checkbox') 
      { 
       if (panel.children[i].checked == true) { 
        counter = counter + 1; 
       } 
      } 
      if (counter > 4) 

      { 
       alert('You have reached to maximum selection'); 
       return false; 

      } 
     } 
    } 
</script> 



<script type="text/javascript"> 

    function aspcheckED0() { 
     document.getElementById("PS0").checked = false; 
    } 
    function aspcheckPS0() { 
     document.getElementById("ED0").checked = false; 

    } 

    function aspcheckEL0() { 
     document.getElementById("HS0").checked = false; 
     document.getElementById("MA0").checked = false; 
    } 
    function aspcheckHS0() { 
     document.getElementById("EL0").checked = false; 
     document.getElementById("MA0").checked = false; 
    } 
    function aspcheckMA0() { 
     document.getElementById("EL0").checked = false; 
     document.getElementById("HS0").checked = false; 
    } 

    function aspcheckUR0() { 
     document.getElementById("PJ0").checked = false; 
    } 
    function aspcheckPJ0() { 
     document.getElementById("UR0").checked = false; 
    } 
    function aspcheckAR0() { 
     document.getElementById("PR0").checked = false; 
    } 
    function aspcheckPR0() { 
     document.getElementById("AR0").checked = false; 
    } 

    </script> 



     <table id="artstable" align="center" class="auto-style53" style="border: thin dotted #6666FF; font-family: Verdana; 
     border-collapse: collapse; background-color: #E6E6E6;"> 
      <tr> 
       <td align="right" class="auto-style188" colspan="10"><strong>Enter Your Admission Details</strong></td> 
      </tr> 
      <tr> 
       <td align="right" class="auto-style42" colspan="5"> 
        <asp:Label ID="lbl" runat="server" Text="Label"></asp:Label> 
        &nbsp;&nbsp;&nbsp;<strong> Select Your Course:</strong></td> 
       <td align="right" class="auto-style42" colspan="5"> 
        <asp:RadioButtonList ID="rbcourse" runat="server" AutoPostBack="True" Height="54px" RepeatDirection="Horizontal" style="text-align: left; font-weight: 700;" Width="385px"> 
         <asp:ListItem>BA</asp:ListItem> 
         <asp:ListItem>BSC</asp:ListItem> 
         <asp:ListItem>BSCN</asp:ListItem> 
         <asp:ListItem>BCOM</asp:ListItem> 
        </asp:RadioButtonList> 
       </td> 
      </tr> 
      <tr> 
       <td align="right" class="auto-style140" style="border: thin dotted #0000FF"><strong>B.A</strong></td> 
       <td align="left" class="auto-style113" colspan="0">Group&nbsp; 1</td> 
       <td class="auto-style162">Group&nbsp; 2</td> 
       <td align="left" class="auto-style114">Group 3</td> 
       <td align="left" class="auto-style95">Group 4</td> 
       <td align="left" class="auto-style150">Group 5</td> 
       <td align="left" class="auto-style174">Group 6</td> 
       <td align="left" class="auto-style160">Group 7</td> 
       <td align="left" class="auto-style136">Group 8</td> 
       <td align="left" class="auto-style116">&nbsp;</td> 
      </tr> 
      <tr> 
       <td align="right" class="auto-style140" style="border: thin dotted #0000FF"><span class="auto-style43">Select Any four </span> 
        <br class="auto-style43" /> 
        <span class="auto-style43">subjects ,Max one from<br /> each group</span></td> 
       <td align="left" class="auto-style117"> 
        <asp:CheckBox ID="ge" runat="server" BackColor="#CCFFFF" Text="GE" onclick="javascript:return ChkValidate();" SkinID="ch" /> 
        <br /> 
       </td> 
       <td align="left" class="auto-style163" rowspan="1"> 
        <asp:CheckBox ID="PS" runat="server" BackColor="#CCFFFF" Text="PS" onclick="aspcheckPS();" SkinID="ch"/> 
        <br class="auto-style139" /> 
        <br class="auto-style139" /> 
        <asp:CheckBox ID="ED" runat="server" BackColor="#CCFFFF" Text="ED" onclick="aspcheckED();" SkinID="ch" /> 
       </td> 
       <td align="left" class="auto-style118"> 
        <asp:CheckBox ID="ec" runat="server" BackColor="#CCFFFF" Text="EC" onclick="javascript:return ChkValidate();" SkinID="ch" /> 
        <br /> 
        <br /> 
       </td> 
       <td align="left" class="auto-style96"> 
        <asp:CheckBox ID="kr" runat="server" BackColor="#CCFFFF" Text="KR" onclick="javascript:return ChkValidate();" SkinID="ch" /> 
        <br /> 
       </td> 
       <td align="left" class="auto-style151" rowspan="1"> 
        <asp:CheckBox ID="MA" runat="server" BackColor="#CCFFFF" Text="MA" onclick="javascript:return ChkValidate();" SkinID="ch" /> 
        <br class="auto-style139" /> 
        <br class="auto-style139" /> 
        <asp:CheckBox ID="EL" runat="server" BackColor="#CCFFFF" Text="EL" onclick="javascript:return ChkValidate();" SkinID="ch"/> 
        <br class="auto-style139" /> 
        <br class="auto-style139" /> 
        <asp:CheckBox ID="HS" runat="server" BackColor="#CCFFFF" Text="HS" onclick="javascript:return ChkValidate();" SkinID="ch"/> 
        <br class="auto-style139" /> 
        <br class="auto-style139" /> 
        <br class="auto-style139" /> 
       </td> 
       <td align="left" class="auto-style175" rowspan="1"> 
        <asp:CheckBox ID="UR" runat="server" BackColor="#CCFFFF" Text="UR" onclick="javascript:return ChkValidate();" SkinID="ch" /> 
        <br class="auto-style139" /> 
        <br class="auto-style139" /> 
        <asp:CheckBox ID="PJ" runat="server" BackColor="#CCFFFF" Text="PJ" onclick="javascript:return ChkValidate();" SkinID="ch" /> 
        <br class="auto-style139" /> 
       </td> 
       <td align="left" class="auto-style161" rowspan="1"> 
        <asp:CheckBox ID="AR" runat="server" BackColor="#CCFFFF" Text="AR" onclick="javascript:return ChkValidate();" SkinID="ch" /> 
        <br class="auto-style139" /> 
        <br class="auto-style139" /> 
        <asp:CheckBox ID="PR" runat="server" BackColor="#CCFFFF" Text="PR" onclick="javascript:return ChkValidate();" SkinID="ch" /> 
       </td> 
       <td align="left" class="auto-style137"> 
        <asp:CheckBox ID="fe" runat="server" BackColor="#CCFFFF" Text="FE" onclick="javascript:return ChkValidate();" SkinID="ch" /> 
       </td> 
      </tr> 
     </table> 

回答

0

這將是jQuery的更容易和更清潔。但是,如果你沒有這個選項,那就這樣做: 編輯:添加警報少於四個選擇。

的JavaScript:

<script type="text/javascript"> 
    function ChkValidate(elem) { 

     ValidateGroup(elem); 
     var valid = Validate(); 
     EnableCheckBoxes(valid); 
    }; 

    function ValidateGroup(elem) { 
     if (elem.type == 'checkbox' && elem.checked) { 
      var parenttd = elem.parentNode.parentNode; 

      var controls = parenttd.getElementsByTagName("input") 
      for (var i = 0; i < controls.length; i++) { 
       if (controls[i].type == 'checkbox') controls[i].checked = false; 
      } 
      elem.checked = true; 
     } 
    }; 

    function EnableCheckBoxes(valid) {    
    var tbl = document.getElementById("artstable") 
    var controls = tbl.getElementsByTagName("input") 

     for (var i = 0; i < controls.length; i++) { 
      if (controls[i].type == 'checkbox') { 
       if (!controls[i].checked) controls[i].disabled = !valid; 
      } 
     } 

    }; 

    function Validate() { 
     var valid = true; 
     var counter = 0;    
     var tbl = document.getElementById("artstable") 
     var controls = tbl.getElementsByTagName("input") 

     for (var i = 0; i < controls.length; i++) { 
      if (controls[i].type == 'checkbox') { 
       if (controls[i].checked) counter++; 
      } 
     } 

     valid = counter < 4; 
     return valid; 
    }; 

    function ValidateSubmission() { 
     if (Validate()) { 
     alert('Please select four subjects'); 
      return false; 
     } 
     return true; 
    }; 
</script> 

ASPX標記:

<table id="artstable" align="center" class="auto-style53" style="border: thin dotted #6666FF; font-family: Verdana; border-collapse: collapse; background-color: #E6E6E6;"> 
    <tr> 
     <td align="right" class="auto-style188" colspan="10"><strong>Enter Your Admission Details</strong></td> 
    </tr> 
    <tr> 
     <td align="right" class="auto-style42" colspan="5"> 
      <asp:Label ID="lbl" runat="server" Text="Label"></asp:Label> 
      &nbsp;&nbsp;&nbsp;<strong> Select Your Course:</strong></td> 
     <td align="right" class="auto-style42" colspan="5"> 
      <asp:RadioButtonList ID="rbcourse" runat="server" AutoPostBack="True" Height="54px" RepeatDirection="Horizontal" Style="text-align: left; font-weight: 700;" Width="385px"> 
       <asp:ListItem>BA</asp:ListItem> 
       <asp:ListItem>BSC</asp:ListItem> 
       <asp:ListItem>BSCN</asp:ListItem> 
       <asp:ListItem>BCOM</asp:ListItem> 
      </asp:RadioButtonList> 
     </td> 
    </tr> 
    <tr> 
     <td align="right" class="auto-style140" style="border: thin dotted #0000FF"><strong>B.A</strong></td> 
     <td align="left" class="auto-style113" colspan="0">Group&nbsp; 1</td> 
     <td class="auto-style162">Group&nbsp; 2</td> 
     <td align="left" class="auto-style114">Group 3</td> 
     <td align="left" class="auto-style95">Group 4</td> 
     <td align="left" class="auto-style150">Group 5</td> 
     <td align="left" class="auto-style174">Group 6</td> 
     <td align="left" class="auto-style160">Group 7</td> 
     <td align="left" class="auto-style136">Group 8</td> 
     <td align="left" class="auto-style116">&nbsp;</td> 
    </tr> 
    <tr> 
     <td align="right" class="auto-style140" style="border: thin dotted #0000FF"><span class="auto-style43">Select Any four </span> 
      <br class="auto-style43" /> 
      <span class="auto-style43">subjects ,Max one from<br /> 
       each group</span></td> 
     <td align="left" class="auto-style117"> 
      <asp:CheckBox ID="ge" runat="server" BackColor="#CCFFFF" Text="GE" onclick="javascript:return ChkValidate(this);" SkinID="ch" /> 
      <br /> 
     </td> 
     <td align="left" class="auto-style163" rowspan="1"> 
      <asp:CheckBox ID="PS" runat="server" BackColor="#CCFFFF" Text="PS" onclick="javascript:return ChkValidate(this);" SkinID="ch" /> 
      <br class="auto-style139" /> 
      <br class="auto-style139" /> 
      <asp:CheckBox ID="ED" runat="server" BackColor="#CCFFFF" Text="ED" onclick="javascript:return ChkValidate(this);" SkinID="ch" /> 
     </td> 
     <td align="left" class="auto-style118"> 
      <asp:CheckBox ID="ec" runat="server" BackColor="#CCFFFF" Text="EC" onclick="javascript:return ChkValidate(this);" SkinID="ch" /> 
      <br /> 
      <br /> 
     </td> 
     <td align="left" class="auto-style96"> 
      <asp:CheckBox ID="kr" runat="server" BackColor="#CCFFFF" Text="KR" onclick="javascript:return ChkValidate(this);" SkinID="ch" /> 
      <br /> 
     </td> 
     <td align="left" class="auto-style151" rowspan="1"> 
      <asp:CheckBox ID="MA" runat="server" BackColor="#CCFFFF" Text="MA" onclick="javascript:return ChkValidate(this);" SkinID="ch" /> 
      <br class="auto-style139" /> 
      <br class="auto-style139" /> 
      <asp:CheckBox ID="EL" runat="server" BackColor="#CCFFFF" Text="EL" onclick="javascript:return ChkValidate(this);" SkinID="ch" /> 
      <br class="auto-style139" /> 
      <br class="auto-style139" /> 
      <asp:CheckBox ID="HS" runat="server" BackColor="#CCFFFF" Text="HS" onclick="javascript:return ChkValidate(this);" SkinID="ch" /> 
      <br class="auto-style139" /> 
      <br class="auto-style139" /> 
      <br class="auto-style139" /> 
     </td> 
     <td align="left" class="auto-style175" rowspan="1"> 
      <asp:CheckBox ID="UR" runat="server" BackColor="#CCFFFF" Text="UR" onclick="javascript:return ChkValidate(this);" SkinID="ch" /> 
      <br class="auto-style139" /> 
      <br class="auto-style139" /> 
      <asp:CheckBox ID="PJ" runat="server" BackColor="#CCFFFF" Text="PJ" onclick="javascript:return ChkValidate(this);" SkinID="ch" /> 
      <br class="auto-style139" /> 
     </td> 
     <td align="left" class="auto-style161" rowspan="1"> 
      <asp:CheckBox ID="AR" runat="server" BackColor="#CCFFFF" Text="AR" onclick="javascript:return ChkValidate(this);" SkinID="ch" /> 
      <br class="auto-style139" /> 
      <br class="auto-style139" /> 
      <asp:CheckBox ID="PR" runat="server" BackColor="#CCFFFF" Text="PR" onclick="javascript:return ChkValidate(this);" SkinID="ch" /> 
     </td> 
     <td align="left" class="auto-style137"> 
      <asp:CheckBox ID="fe" runat="server" BackColor="#CCFFFF" Text="FE" onclick="javascript:return ChkValidate(this);" SkinID="ch" /> 
     </td> 
    </tr> 
</table> 

<asp:Button ID="btnSubmit" OnClick="btnSubmit_Click" OnClientClick=" return ValidateSubmission();" Text="Submit" runat="server" /> 

我已經測試在FF的代碼,但應在其他瀏覽器中工作。

+0

這段代碼在ie和ff thanx中都能正常工作以便及時幫助兄弟,但是當我檢查第一個複選框時,同一頁中的某個不同表格的其他複選框也會啓用,但這仍然存在問題。默認情況下,他們保持禁用,直到除非藝術,我不會選擇單選按鈕。只有在arts表中選擇複選框時,如何使它們失效。 – arshad

+0

在hort我只需要這個Java腳本只用於藝術表,而不是其他表在這個頁面 – arshad

+0

@arshad - 編輯我的答案在「artstable」表內的checkboex上操作。 – afzalulh

相關問題