0

I have a lot of checkboxes, with the same id="cbna", but when i submit the form , the javascript code should check If atleat one checkbox is checked, if not an alert should pop up. if it is checked the the value stored in site the checkbox should execute along with the details of a text area. I have tried to do the same but in vain.

    <script>

 function checkCheckBoxes(form1) 
     {
    if (document.form1.checked==true)
              {
        function OpenWindow(info)
               {
                   window.open(info);
               }
              }
else
    {
 alert ('You didn\'t choose any of the checkboxes!');
 return false;
    }
}
</script>

<form name="form1" id="form1" method="post" action="" >
  <p>
    <input type="checkbox" name="checkbox" onclick="if(this.checked)OpenWindow(this.value)}" value="www.something.com 1" />
  google<br />
 <input type="checkbox" name="checkbox" onclick="if(this.checked){OpenWindow(this.value)}" value="www.something.com 2" />
  apple<br />

 <input type="checkbox" name="checkbox" onclick="if(this.checked){OpenWindow(this.value)}" value="www.something.com 3" />
  ibm<br />

 <input type="checkbox" name="checkbox" onclick="if(this.checked){OpenWindow(this.value)}" value="www.something.com 4" />
  nageo<br />

 <textarea cols="100" rows="22" id="descp" >
 hello wats up!
</textarea>
 <input onclick="checkCheckBoxes(form1)" value="create forms" type="button">
</form>
</body>
</html>
4

1 回答 1

0

嘿@HatakeKakashi 使用以下代码解决您的问题

page1.html 代码

        <html>
            <head>
                <title>page1</title>
            </head>
             <script>
                     function checkCheckBoxes() 
                    {
                    var flag=false;
                       var elLength = document.form1.elements.length;

                        for (i=0; i<elLength; i++)
                        {
                            var type = form1.elements[i].type;
                            if (type=="checkbox" && form1.elements[i].checked){

                                flag =true;
                            }
                            else if (type=="checkbox") {
                               // alert("not checked");
                            }
                        }

                        if(flag)
                        {
                         window.open("page2.html?textVal="+document.form1.descp.value,'mywindow','width=400,height=200')
                        }
                        else
                        {
                            alert ("You didn\'t choose any of the checkboxes!");
                            return false;
                        }
                   }
        </script>
            <body>
                           <form name="form1" id="form1" method="post" action="" >
                      <p>
                        <input type="checkbox" name="checkbox1" onclick="" value="www.something.com 1" />
                      google<br />
                     <input type="checkbox" name="checkbox1" onclick="" value="www.something.com 2" />
                      apple<br />

                     <input type="checkbox" name="checkbox1" onclick="" value="www.something.com 3" />
                      ibm<br />

                     <input type="checkbox" name="checkbox1" onclick="" value="www.something.com 4" />
                      nageo<br />

                     <textarea cols="100" rows="22" id="descp" >hello wats up!</textarea>
                     <input onclick="checkCheckBoxes(form1)" value="create forms" type="button">
                    </form>
            </body>
        </html> 

page2.html 代码

    <html>
        <head>
          <title>Page2</title>
          <script>
          function getURLParameters(paramName) 
                {
                        var sURL = window.document.URL.toString();  
                    if (sURL.indexOf("?") > 0)
                    {
                       var arrParams = sURL.split("?");         
                       var arrURLParams = arrParams[1].split("&");      
                       var arrParamNames = new Array(arrURLParams.length);
                       var arrParamValues = new Array(arrURLParams.length);     
                       var i = 0;
                       for (i=0;i<arrURLParams.length;i++)
                       {
                        var sParam =  arrURLParams[i].split("=");
                        arrParamNames[i] = sParam[0];
                        if (sParam[1] != "")
                            arrParamValues[i] = unescape(sParam[1]);
                        else
                            arrParamValues[i] = "No Value";
                       }

                       for (i=0;i<arrURLParams.length;i++)
                       {
                                if(arrParamNames[i] == paramName){
                            //alert("Param:"+arrParamValues[i]);
                                return arrParamValues[i];
                             }
                       }
                       return "No Parameters Found";
                    }

                }
          </script>
        </head>
        <body>
           <script>
                    document.write(getURLParameters("textVal"))
           </script>

        </body>
    </html> 
于 2013-01-01T04:20:50.127 回答