2014-03-19 32 views
0

如何僅使用java腳本實現從複選框列表中選中的一個複選框。如何僅實現一個複選框選擇功能到Chekc框列表?

下面是腳本:

<script language="javascript" type="text/javascript"> 
var previousCheckId; 
    function toggle(chkBox) { 
     if (chkBox.checked) { 
       if (previousCheckId) { 
        document.getElementById(previousCheckId).checked = false; 
       } 
       previousCheckId = chkBox.getAttribute('id'); 
     } 
    } 
</script> 

這裏是我的設計:

<tr> 
    <td class="BlackTextBold" align="right" height="25" width="32%"><div align="left"><strong> 
     Product :</strong><span class="top-txt"><span style="font-size: 12.0pt; font-family: Tahoma,sans-serif; color: red">*</span></span></div></td> 
    <td height="20" width="68%"> 
     <asp:CheckBoxList ID="chkLst" runat="server" Font-Bold="True" 
      RepeatDirection="Horizontal" Width="50%"> 
      <asp:ListItem onClick="toggle(this);" Value="EasyOFFICE">EasyOFFICE</asp:ListItem> 
      <asp:ListItem onClick="toggle(this);" Value="EasyVAT">EasyVAT</asp:ListItem> 
     </asp:CheckBoxList> 
    </td> 
    </tr> 
    <tr> 
    <td class="BlackTextBold" align="right" height="25" width="32%"><div align="left"><strong> 
     PC Type :</strong><span class="top-txt"><span style="font-size: 12.0pt; font-family: Tahoma,sans-serif; color: red">*</span></span></div></td> 
    <td height="20" width="68%"> 
     <asp:CheckBoxList ID="chkLst" runat="server" Font-Bold="True" 
      RepeatDirection="Horizontal" Width="30%"> 
      <asp:ListItem onClick="toggle(this);" Value="Server">Server</asp:ListItem> 
      <asp:ListItem onClick="toggle(this);" Value="LAN">LAN</asp:ListItem> 
     </asp:CheckBoxList> 
    </td> 
    </tr> 

但是該腳本正常工作與一個複選框列表,但是當我只是想同樣的事情到其他複選框列表,然後它適用於4個。

+0

爲什麼又使用相同的清單?這段代碼正在編譯?至少更改第二個複選框列表的名稱。 –

回答

0

這是什麼單選按鈕是...

,但如果你想繼續使用複選框,基於jQuery的解決辦法只能是目標在同一td

function toggle(chkBox) { 
    if (chkBox.checked) { 
     $(this).closest('td').find('input[type="checkbox"]:checked').not(this).prop('checked', false) 
    } 
} 
0

首先,由於CheckBoxList的名稱相同,因此不會編譯代碼。 更改第二個複選框列表的名稱,然後嘗試運行腳本,因爲腳本沒有問題。

<tr> 
<td class="BlackTextBold" align="right" height="25" width="32%"><div align="left"><strong> 
    Product :</strong><span class="top-txt"><span style="font-size: 12.0pt; font-family: Tahoma,sans-serif; color: red">*</span></span></div></td> 
<td height="20" width="68%"> 
    <asp:CheckBoxList ID="chkLst" runat="server" Font-Bold="True" 
     RepeatDirection="Horizontal" Width="50%"> 
     <asp:ListItem onClick="toggle(this);" Value="EasyOFFICE">EasyOFFICE</asp:ListItem> 
     <asp:ListItem onClick="toggle(this);" Value="EasyVAT">EasyVAT</asp:ListItem> 
    </asp:CheckBoxList> 
</td> 
</tr> 
    <tr> 
<td class="BlackTextBold" align="right" height="25" width="32%"><div align="left"><strong> 
    PC Type :</strong><span class="top-txt"><span style="font-size: 12.0pt; font-family: Tahoma,sans-serif; color: red">*</span></span></div></td> 
<td height="20" width="68%"> 
    <asp:CheckBoxList ID="chkLst1" runat="server" Font-Bold="True" 
     RepeatDirection="Horizontal" Width="30%"> 
     <asp:ListItem onClick="toggle(this);" Value="Server">Server</asp:ListItem> 
     <asp:ListItem onClick="toggle(this);" Value="LAN">LAN</asp:ListItem> 
    </asp:CheckBoxList> 
</td> 

+0

好吧,我改變了它。但這裏的問題是這個切換適用於所有4個複選框,所以我只能從這4個複選框中選中一個複選框。在這裏我只想從chklst(1'st CheckBox)和其他只有一個從chklst1(2'nd CheckBox)複選框中選中一個複選框。 –

相關問題