Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to check same value 1

Status
Not open for further replies.

Ngai88

Programmer
Sep 8, 2004
56
US
Hello,
I have a drop box that has a list of all the Recipients. Below the drop box, I also have checkboxes for the same list of Recipients. Once a recipient is selected from the drop box, I want the same(matching one) recipient's checkbox automatically checked. I am not sure how to do this. Is this something like the following? If not, what is the correct to do this?

form.<%=MComposeController.RECIPIENT%>[form.<%=MComposeController.SUBJECT%>.value].checked


Thanks,
Ngai
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Below is the javascript for it...


<script type="text/javascript">
function verifyForm(form) {
var error = "Error(s):\n\n";
if (form.<%=MComposeController.SUBJECT %>.value == "") {
error += "<%=MComposeController.SUBJECT %> is a required field.\n";
}
var hasRecipient;

for(i=0; i < form.<%=MComposeController.RECIPIENT %>.length; i++) {
if (form.<%=MComposeController.RECIPIENT %>.checked) {
hasRecipient = true;
break;
}
}
if (!hasRecipient) {
error += "<%=MComposeController.RECIPIENT %> is a required field.\n";
}
if (error != "Error(s):\n\n") {
alert(error);
return false;
}
}
</script>
 
Code:
<html>
<head>
<script lang="JavaScript">
var chklen=2; // number of checkboxes
function checkOneMore()
{
if (document.f1.s1.selectedIndex!=-1)
   {
    checkTemp = eval("document.f1.check"+document.f1.s1.selectedIndex);
    checkTemp.checked = true;
   }
}
function checkOnlyOne()
{
for (i=0;i<=chklen-1;i++)
    {
    checkTemp = eval("document.f1.check"+i);
    checkTemp.checked = false;
    }
if (document.f1.s1.selectedIndex!=-1)
   {
    checkTemp = eval("document.f1.check"+document.f1.s1.selectedIndex);
    checkTemp.checked = true;
   }
}
</script>
</head>
<body>
<form name="f1">
<select name="s1" onChange='javascript:checkOnlyOne()'>
<option value="1">one</option>
<option value="2">two</option>
</select>
<input type="checkbox" name="check0" value="cone">
<input type="checkbox" name="check1" value="ctwo">
</form>
</body>
</html>
Code:
<%!
int checkboxNum = 4;
String selectName = "s1";
String checkboxName = "check";
String checkValue[] = {"czero","cone","ctwo","cthree"};
%>
<html>
<head>
<script lang="JavaScript">
var chklen=<%=checkboxNum%>; // number of checkboxes
function checkOneMore()
{
if (document.f1.<%=selectName%>.selectedIndex!=-1) //
   {
    checkTemp = eval("document.f1.<%=checkboxName%>"+document.f1.<%=selectName%>.selectedIndex); //
    checkTemp.checked = true;
   }
}
function checkOnlyOne()
{
for (i=0;i<=chklen-1;i++)
    {
    checkTemp = eval("document.f1.<%=checkboxName%>"+i); //
    checkTemp.checked = false;
    }
if (document.f1.<%=selectName%>.selectedIndex!=-1) //
   {
    checkTemp = eval("document.f1.<%=checkboxName%>"+document.f1.<%=selectName%>.selectedIndex); //
    checkTemp.checked = true;
   }
}
</script>
</head>
<body>
<form name="f1">
<select name="<%=selectName%>" onChange='javascript:checkOnlyOne()'>
<%
for (int i=0;i<checkboxNum;i++)
     out.println("<option value=\""+i+"\">item"+i+"</option>");
%>
</select>
<%
for (int i=0;i<checkboxNum;i++)
     out.println("<input type=\"checkbox\" name=\""+checkboxName+i+"\" value=\""+checkValue[i]+"\">");
%>
</form>
</body>
</html>
 
Thank you Prosper..(o:
The version I edited after I submitted the post is not working as smoothly as I wanted it to be. I am glad you show me the proper way of doing this. I'll make changes on mine using yours as guideline. Once again thanks a million.

Ngai
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top