2 questions about checkbox into form

Discussion in 'ASP General' started by Fred, Oct 24, 2004.

  1. Fred

    Fred Guest

    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
     
    Fred, Oct 24, 2004
    #1
    1. Advertising

  2. Fred

    thorpe Guest

    what makes you think this has anything to do with asp?

    "Fred" <> wrote in message
    news:u#...
    > 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
    >
    >
    >
    >
     
    thorpe, Oct 24, 2004
    #2
    1. Advertising

  3. 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"
     
    Bob Barrows [MVP], Oct 24, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tshad
    Replies:
    0
    Views:
    536
    tshad
    Apr 15, 2005
  2. Vikram

    disable checkbox list checkbox

    Vikram, Jan 25, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    1,407
    ThunderMusic
    Jan 25, 2006
  3. =?Utf-8?B?RG90TmV0RGV2?=

    DataGrid and embeded Checkbox..How to find if checkbox clicked

    =?Utf-8?B?RG90TmV0RGV2?=, Oct 6, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    832
    David Wier
    Oct 6, 2006
  4. Jason Huang
    Replies:
    0
    Views:
    560
    Jason Huang
    Jun 29, 2007
  5. Joey Pang
    Replies:
    4
    Views:
    526
    Joey Pang
    Jun 13, 2005
Loading...

Share This Page