...

View Full Version : Allow One Check box checked..



tanpl3
11-06-2004, 09:15 AM
i have multiple check box but i only want ONE check box is checked when the onclick method apply, the rest unchecked, while all the check box is same name....is there any solution??

Puffin the Erb
11-06-2004, 01:05 PM
Here is one possibility :

<html>
<head>
<title>Checkboxes</title>
<script type="text/javascript">
function resetChecks(currentCheckBox)
{
f = document.forms[0];

for(i=0; i<f.elements.length;i++)
{
if(f.elements[i].type!="checkbox" || i==currentCheckBox)
continue;
f.elements[i].checked=false;
}
}

</script>
</head>

<body>
<form>
<input type="checkbox" onClick="resetChecks(0)" name="choice">
<input type="checkbox" onClick="resetChecks(1)" name="choice">
<input type="checkbox" onClick="resetChecks(2)" name="choice">
</form>
</body>
</html>

kansel
11-06-2004, 06:21 PM
Why not use a radiobutton group for this? Its natural behavior is exactly as you described, no javascript required.
<form>
<fieldset><legend>group1</legend>
<input type="radio" name="group1" value="1">
<input type="radio" name="group1" value="2">
<input type="radio" name="group1" value="3"></fieldset>
<fieldset><legend>group2</legend>
<input type="radio" name="group2" value="1">
<input type="radio" name="group2" value="2">
<input type="radio" name="group2" value="3"></fieldset>
</form>

tanpl3
11-08-2004, 02:55 AM
Actually i am trying to do in this way, everytime, the system will loop thru the database and get the matching data, then place in table, so, i wouldn't know how many checkbox in a time. Once i click on either one of the checkbox, it will redirect to the respective page.
<%
if rs.bof <> true and rs.eof <> true then
rs.Movefirst
i = 1
while not rs.eof
%>
<tr>
<td width="5%" height="17" background="../../../images/loan/bg1.gif">
<input type="checkbox" name="chkbox" value="ON"></td>
<td width="12%" height="17"><%=rs("acctno")%></td>
<td width="1%" height="17"><%=rs("cifno")%></td>
<td width="30%" height="17"><%=rs("cfsnme")%></td>
<td width="15%" height="17">&nbsp;</td>
</tr>
<%
i = i + 1
rs.movenext
wend
%>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum