Single-choice events in the HTML-CSS Group"

Source: Internet
Author: User

Because of a single choice button problem of death, N people come out, alas, even this cainiao-level character also came out, mixed water to fish. One accidentally touched one.

First, let's look at a piece of code that has been dug out from baidu.Copy codeThe Code is as follows: <script>
Function checkradio ()
{
For (I = 0; I <document. form1.Fruit. length; I ++)
{
If (document. form1.Fruit [I]. checked)
{
Alert ("your favorite fruit is:" + document. form1.Fruit [I]. nextSibling. nodeValue );
}
}
}
</Script>
<Form name = "form1">
Your favorite fruit is: <br>
<Input type = radio value = "Fruit1" name = "Fruit" checked> Apple
<Input type = radio value = "Fruit2" name = "Fruit"> banana
<Input type = radio value = "Fruit3" name = "Fruit"> strawberry
<Input type = radio value = "Fruit4" name = "Fruit"> pineapple
<Input type = button value = "select" onclick = "checkradio ()">
</Form>

This code is used to determine the selected value.

The result is that if every option is empty, a prompt should be given. The following code is the result after the change.

Program code
<Script>
Function checkradio ()
{
Var j = 0;
For (I = 0; I <document. form1.Fruit. length; I ++)
{
If (document. form1.Fruit [I]. checked = true)
{
Alert ("you have selected" + document. form1.Fruit [I]. nextSibling. nodeValue );
// Break; this break is not valid because it has been instructed by Feifei. Remove it.
} Else {
J = j + 1;
If (j = 4 ){
Alert! ");
}
}
}
}
</Script>
<Form name = "form1">
Your favorite fruit is: <br>
<Input type = radio value = "Fruit1" name = "Fruit">
Apple
<Input type = radio value = "Fruit2" name = "Fruit"> banana
<Input type = radio value = "Fruit3" name = "Fruit"> strawberry
<Input type = radio value = "Fruit4" name = "Fruit"> pineapple
<Input type = button value = "select" onclick = "checkradio ()">
</Form>

Later I saw a clearer piece of code written in the morning. I will post it here.
(Note: To facilitate the test, I changed the idea)Copy codeThe Code is as follows: <script>
Function checkradio ()
{
Var flag = false;
For (var I = 0; I <= document. form1.Fruit. length-1; I ++)
{
If (form1.Fruit [I]. checked ){
Flag = true ;}
}
If (flag)
{
Alert ("^_^ ");
Return false;
} Else {
Alert ("hero, you should choose one! ");
}
}
</Script>

The single choice is over, and Feifei's boss is not dead-hearted. At night, a code for the check box is developed.Copy codeThe Code is as follows: <script>
Var j = document. getElementsByName ("Fruit ");
Function allche (){
For (var I = 0; I <j. length; I ++ ){
If (document. form1.Fruit [I]. checked! = True) document. form1.Fruit [I]. checked = document. form1.suoy. checked;
If (document. form1.Fruit [I]. checked = true) document. form1.Fruit [I]. checked = document. form1.suoy. checked;
}
}
Function checkall (){
Var aa = 0
For (var I = 0; I <j. length; I ++ ){
If (document. form1.Fruit [I]. checked = true) aa ++;
Aa! = J. length? Document. form1.suoy. checked = false: document. form1.suoy. checked = true;
}
}
Function checkradio (){
Var a = 0
Var list = ""
For (var I = 0; I <j. length; I ++)
If (document. form1.Fruit [I]. checked = true ){
List = ""? List = document. form1.Fruit [I]. value: list = list + "," + document. form1.Fruit [I]. value;
}
If (list! = "") Alert ("your favorite fruit is" + list );

Else {
A ++;
If (a = j. length) alert ("Eldest Brother. How do you know what you like? ");}
}

</Script>
<Form name = "form1" id = "frm">
Your favorite fruit is: <br>
<Input type = checkbox value = "apple" name = "Fruit" onClick = "checkall ()">
Apple
<Input type = checkbox value = "banana" name = "Fruit" onClick = "checkall ()">
Bananas
<Input type = checkbox value = "strawberry" name = "Fruit" onClick = "checkall ()">
Strawberry
<Input type = checkbox value = "pineapple" name = "Fruit" onClick = "checkall ()">
Pineapple
<Input type = button value = "select" onclick = "checkradio ()">
<Input type = checkbox onclick = "allche ()" name = "suoy"> select all
</Form>

<Script language = "javascript" type = "text/javascript" id = "commonjs">
Function test ()
{
Fruitlist = "";
For (I = 0; I <document. getElementById ("frm"). length; I ++)
If (document. getElementById ("frm") [I]. type = "checkbox" & document. getElementById ("frm") [I]. checked)
Fruitlist + = document. getElementById ("frm") [I]. value + "";
If (fruitlist! = "")
Alert ("your favorite fruit is" + fruitlist );
Else
Alert ("big brother. How do you know what you like? ");
}
</Script>

Let's look at a more concise code and code with better effects on the check.Copy codeThe Code is as follows: <script language = "JavaScript">
<! -- Begin
Function checkAll (){
For (var j = 1; j <= 9; j ++ ){
Box = eval ("document. checkboxform. C" + j );
If (box. checked = false) box. checked = true;
}
}

Function uncheckAll (){
For (var j = 1; j <= 9; j ++ ){
Box = eval ("document. checkboxform. C" + j );
If (box. checked = true) box. checked = false;
}
}

Function switchAll (){
For (var j = 1; j <= 9; j ++ ){
Box = eval ("document. checkboxform. C" + j );
Box. checked =! Box. checked;
}
}
// End -->
</Script>
</Head>
<Body>
<Form name = checkboxform>
<Input type = checkbox name = C1> C1 <br>
<Input type = checkbox name = C2> C2 <br>
<Input type = checkbox name = C3> C3 <br>
<Input type = checkbox name = C4> C4 <br>
<Input type = checkbox name = C5> C5 <br>
<Input type = checkbox name = C6> C6 <br>
<Input type = checkbox name = C7> C7 <br>
<Input type = checkbox name = C8> C8 <br>
<Input type = checkbox name = C9> C9 <br>
<Br>
<Input type = button value = "select all" onClick = "checkAll ()"> <br>
<Input type = button value = "cancel" onClick = "uncheckAll ()"> <br>
<Input type = button value = "invert" onClick = "switchAll ()"> <br>
</Form>

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.