SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Evangelist Miguel61's Avatar
    Join Date
    Mar 2008
    Posts
    402
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)

    Control for checkboxes form not working

    Hi there.

    This control function Button1_onclick() for checkboxes in the my form not working, why?
    Can u help me?

    Code:
    <HTML>
    <HEAD>
    <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
     
        <script language="javascript" type="text/javascript">
    // <!CDATA[
     
    function Button1_onclick() 
    {
        if (!controllo_check())
        {
             alert("Stop");
            return;
        }
        document.forms[0].submit();
     
    }
     
     
    function controllo_check()
    {
        var cks = document.forms[0].chkParent;
        var checked = false;
        for(var i = 0; i < cks.length; i++)
        {
            if(cks[i].checked)
            {
                checked = true;
                break;
            }
        }
        return checked; 
     
    }
     
     
    // ]]>
        </script>
     
    </HEAD>
    <title>Multiple checkbox select sample</title>
    <BODY>
     
    
    <form name=frmTest action="default-2.asp" method=POST onsubmit="return(Button1_onclick(this));" >
     
    <input type=CHECKBOX name=chkParent1 value="3742">karla_c26<br>
     
    <input type=CHECKBOX name=chkParent2 value="4430">AleAlvarez<br>
     
    <input type=CHECKBOX name=chkParent3 value="3303">Alejandra<br>
     
    <input type=CHECKBOX name=chkParent4 value="3720">Angio<br>
     
    <input type=CHECKBOX name=chkParent5 value="3">gino<br>
     
    <input type=CHECKBOX name=chkParent6 value="4606">Andrea<br>
     
    <input type=CHECKBOX name=chkParent7 value="3962">alx<br>
     
    <input type=CHECKBOX name=chkParent8 value="4095">ANGELADAUTO<br>
     
    <input type=CHECKBOX name=chkParent9 value="7">cas<br>
     
    <input type=CHECKBOX name=chkParent10 value="8">assim<br>
     
    <input type=HIDDEN name=txtCount value="10">
    <p><input type=SUBMIT value="Select"></p>
    </form>
    </BODY>
    </HTML>

  2. #2
    SitePoint Zealot moretea's Avatar
    Join Date
    Jun 2004
    Location
    Texas, USA
    Posts
    118
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Just a guess, but I think you need to rename all the checkbox elements with the same name, "chkParent". If you need to refer to a specific element within the array, you could call it by chkParent[x], or you could also add an id attribute for each one, using unique id values in the way you are setting your name values now.

    Good luck!

  3. #3
    SitePoint Evangelist Miguel61's Avatar
    Join Date
    Mar 2008
    Posts
    402
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Thank You for ur help !!!

    This is the solution:

    Code:
        <script language="javascript" type="text/javascript">
    // <!CDATA[
    
    function atLeastOneChecked(form) {
    
       var inputs = form.getElementsByTagName('input');
       var i = inputs.length;
       while (i--) {
    
       if (inputs[i].checked && !inputs[i].disabled) return confirm("Are you sure?");     
    
       }
       
       alert("ko!")
       return false;
    
    }
    
    // ]]>
        </script>
    
    ...
    <form name=frmTest action="default-2.asp" method=POST onsubmit="return atLeastOneChecked(this)">
    ...
    </form>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •