473,386 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

2 questions about checkbox into form

Hi,

I defined a form consisting of checkboxes like:
<form>
<input type="checkbox" name=ck id=ck onclick="check(this.form)"
<input type="checkbox" name=ck id=ck onclick="check(this.form)"
.....
<input type="checkbox" name=ck id=ck onclick="check(this.form)"
</form>

1) when this function is ran, if there is only ONE checkbox into the form, i
get in the first ALERT: "undefined" and the second gives a value of 0. When
there are 2 ore more, i get the exact length and the right number of checked
checkboxes..
My question is: why and how to solve this?
function check(form)
{
var total = 0;
var mx = form.ck.length;
alert(mx)

for (var x = 0; x < mx; x++)
{
if (eval("document.chk.ck[" + x + "].checked") == true)
{ total += 1 }
}
alert(total)
2) When one checkbox is checked, i would like to disabeld all the others,
because only one may be checked.
I tried different ways, but no succesfully (in the same function as above):
"impossible to affect to a result of function" (or something ..)

if (total==1)
{
for (var x = 0; x < mx; x++)
{
if (eval("document.chk.ck[" + x + "].checked") == false)
{eval("document.chk.ck[" + x + "].disabled") = true}
}
}

Thanks again for any hints
Fred


Jul 21 '05 #1
2 1884
what makes you think this has anything to do with asp?

"Fred" <ff*@its.gb> wrote in message
news:u#*************@TK2MSFTNGP15.phx.gbl...
Hi,

I defined a form consisting of checkboxes like:
<form>
<input type="checkbox" name=ck id=ck onclick="check(this.form)"
<input type="checkbox" name=ck id=ck onclick="check(this.form)"
....
<input type="checkbox" name=ck id=ck onclick="check(this.form)"
</form>

1) when this function is ran, if there is only ONE checkbox into the form, i get in the first ALERT: "undefined" and the second gives a value of 0. When there are 2 ore more, i get the exact length and the right number of checked checkboxes..
My question is: why and how to solve this?
function check(form)
{
var total = 0;
var mx = form.ck.length;
alert(mx)

for (var x = 0; x < mx; x++)
{
if (eval("document.chk.ck[" + x + "].checked") == true)
{ total += 1 }
}
alert(total)
2) When one checkbox is checked, i would like to disabeld all the others,
because only one may be checked.
I tried different ways, but no succesfully (in the same function as above): "impossible to affect to a result of function" (or something ..)

if (total==1)
{
for (var x = 0; x < mx; x++)
{
if (eval("document.chk.ck[" + x + "].checked") == false)
{eval("document.chk.ck[" + x + "].disabled") = true}
}
}

Thanks again for any hints
Fred

Jul 21 '05 #2
As thorpe says, this has nothing to do with ASP, and should have been posted
to a javascript or client-side scripting newsgroup, such as
..scripting.jscript, or one of the groups with "dhtml" in their names.
However, I will attempt to answer this below, so that your time will not
have been totally wasted. Please followup in the appropriate newsgroup
however. (cc'ed and Followup-to set to microsoft.public.scripting.jscript)
Fred wrote:
Hi,

I defined a form consisting of checkboxes like:
<form>
<input type="checkbox" name=ck id=ck onclick="check(this.form)"
<input type="checkbox" name=ck id=ck onclick="check(this.form)"
....
<input type="checkbox" name=ck id=ck onclick="check(this.form)"
You should give each checkbox a unique id, keeping the names the same:
<input type="checkbox" name=ck id=ck1 onclick="check(this.form)"
<input type="checkbox" name=ck id=ck2 onclick="check(this.form)"
.....
<input type="checkbox" name=ck id=ck4 onclick="check(this.form)"

</form>

1) when this function is ran, if there is only ONE checkbox into the
form, i get in the first ALERT: "undefined" and the second gives a
value of 0. When there are 2 ore more, i get the exact length and the
right number of checked checkboxes..
My question is: why and how to solve this?
function check(form)
{
var total = 0;
var mx = form.ck.length;
alert(mx)

for (var x = 0; x < mx; x++)
{
if (eval("document.chk.ck[" + x + "].checked") == true)
Get out of the habit of using eval. It is not necessary. A better technique
in this case would be:

var cks=document.getElementsByName("ck"),total = 0
for (var x = 0; x < cks.length; x++)
{
if (cks[x].checked == true)
{total += 1}
}

alert(total);

2) When one checkbox is checked, i would like to disabeld all the
others, because only one may be checked.
I tried different ways, but no succesfully (in the same function as
above): "impossible to affect to a result of function" (or something
..)
The best way to do this would be to use radio buttons instead of checkboxes.
The page would handle this for you. However,

if (total==1)
{
for (var x = 0; x < mx; x++)
{
if (eval("document.chk.ck[" + x + "].checked") == false)
{eval("document.chk.ck[" + x + "].disabled") = true}
}
}


Again with the eval! ugh! Try this:

<HTML>
<HEAD>
<SCRIPT type="text/javascript">
function check(obj)
{
var cks=document.getElementsByName("ck")
for (var x = 0; x < cks.length; x++)
{
if (cks[x].id != obj.id)
{
cks[x].checked=false;
cks[x].disabled=obj.checked;
}
}
}
</SCRIPT>
</HEAD>
<BODY>
<form id="frm1" method="post">
<INPUT type="checkbox" id="ck1" name="ck" value="1" onclick="check(this)">
<INPUT type="checkbox" id="ck2" name="ck" value="2" onclick="check(this)">
<INPUT type="checkbox" id="ck3" name="ck" value="3" onclick="check(this)">
<INPUT type="checkbox" id="ck4" name="ck" value="4" onclick="check(this)">
<INPUT type="submit" value="Submit" id=submit1 name=submit1>

</form>


Bob Barrows

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Jul 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jack | last post by:
Hi, I have a checkbox the value which goes to a database via a asp page that builds the sql string. In the front end asp page, the checkbox code is written as follows: <i><input...
3
by: Ondernemer | last post by:
Hi guys, On my page I dynamically create different checkboxes. <input type="checkbox" name="ch1" value="some value"> option 1 <input type="checkbox" name="ch2" value="some value"> option 2...
4
by: feanor | last post by:
I need to select children checkboxes when selecting the parent one. This is my function: function SelectChildrens(checkbox_name){ form = document.forms; Sname = checkbox_name.split("-"); for...
2
by: Mark | last post by:
I am trying to use a Pagable Repeater with checkboxes in ASP.Net, I cannot seem to associate the checkbox with a particular database record so I lose the checked state from page-to-page. Below is...
1
by: dbuchanan | last post by:
Hello, A section in Data Sources window is mystifying to me. In the case of my code the Data Sources window shows my references to the data access layer. First here is what I see in my Data...
7
by: Don | last post by:
I'm not very proficient with Access, probably only enough to make me dangerous. With that said I've got two questions. #1 (probably pretty simple). I know I've done this before but how do I get...
0
by: cyberdawg999 | last post by:
Greetings all in ASP land I have overcome one obstacle that took me 2 weeks to overcome and I did it!!!!! I am so elated!! thank you to all who invested their time and energy towards helping me...
0
by: TechnoAtif | last post by:
<?php include "dbconnect.php"; include "commonFunc.php"; ?> <!----------------------------------> <table width="80%" border="1" cellpadding="2" cellspacing="0"> <tr > <td...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.