About JavaScript control check box Select all, you must choose one, control the number of code

Source: Internet
Author: User

<script type= "Text/javascript Tutorial" >

function Check ()
{
var F=document.getelementbyid ("Form1");
var obj=document.getelementsbyname ("User");
for (Var i=0;i<obj.length;i++)
{
if (obj[i].checked==true)
{
var flag= "OK";
Break
}
}
if (flag!= "OK")
{
Alert ("Please select Recipient!") ");
return false;
}
Else
{
return true;
}
}

</script>

Html:

<input type= "checkbox" name= "User" value= "1"/>
<input type= "checkbox" name= "User" value= "2"/>
<input type= "Submit" Name= "Submit1" onclick= "return Check ()" value= "Send"/>

<script language=javascript Tutorials >    
///First method     

function Selectall1 ()
{
var a = document.getElementsByTagName ("input");
if (a[0].checked==true) {
for (var i=0 i<a.length; i++) <= "span=" >
if (A[i].type = = "checkbox") a[i].checked = false;
}
Else
{
for (var i=0 i<a.length; i++) <= "span=" >
if (A[i].type = = "checkbox") a[i].checked = true;
}
}  


The second method

function Selectall2 () {
var tform = document.forms[' Form1 '];
for (Var i=0;i<tform.length;i++) <= "span=" >
{
var e = tform.elements[i];
if (E.type = "checkbox")
e.checked =!e.checked;
}
}

The third method, combined with both of these methods

function Selectall3 ()
{
var a = document.getElementsByTagName ("input");
for (var i=0 i<a.length; i++) <= "span=" >
if (A[i].type = = "checkbox") a[i].checked =!a[i].checked;
}    


Fourth method

function Selectall4 (ID) {//ID Distinguished
var tform=document.forms[' Form1 '];
for (Var i=0;i<tform.length;i++) {<= "" span= "" >
var e=tform.elements[i];
if (e.type== "checkbox" && e.>}
}    


Fifth method

function SelectAll (theform,thename) {//theform specified form,thename is the Name property of the checkbox
var tform=document.forms[theform];
document.getElementById ("Thewen"). Value= ' reverse election ';
for (Var i=0;i<tform.length;i++) {<= "" span= "" >
var e=tform.elements[i];
if (e.type== ' checkbox ' && e.>}
}    


</script>

Controlling the number of checkbox

<script language= "JavaScript" >
function cc ()
{
var Oc=document.form1.sitem;
var t=0;


if (OC)
{
for (Var i=0;i<oc.length;i++) {
if (OC (i). Checked) t++
}
}

if (t>7 && t<9) alert ("You can only choose 7, but you chose" + t + "");
}

</script>


<body>

<script language= "JavaScript" >

</script>

<form name= "Form1" Id= "Form1" action= "nextpage.htm" >
Please select up to 7 items:
<p>
<input type= "checkbox" Value= "CRC" Name= "Sitem" > Www.111cn.net <br/>
<input type= "checkbox" value= "Northsnow" name= "Sitem" id= "CheckBox1" > Northern Snow <br/>
<input type= "checkbox" value= "Csdn" name= "Sitem" id= "CheckBox2" > China's largest developer network <br/>
<input type= "checkbox" Value= "a" name= "Sitem" id= "Checkbox3" > Fly <br/>
<input type= "checkbox" value= "B" name= "Sitem" id= "checkbox4" > Snow <br/>
<input type= "checkbox" Value= "C" name= "Sitem" id= "checkbox5" > Lian <br/>
<input type= "checkbox" value= "D" name= "Sitem" id= "checkbox6" > Day <br/>
<input type= "checkbox" Value= "E" name= "Sitem" id= "checkbox7" > Shot <br/>
<input type= "checkbox" value= "F" name= "Sitem" id= "checkbox8" > White <br/>
<input type= "checkbox" value= "G" name= "Sitem" id= "checkbox9" > Deer <br/>
<input type= "button" name= "ss" value= "OK" onclick= "cc ();" >
</form>
</body>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.