Use Javascript to unselect RadioButtonList

Source: Internet
Author: User

The Helper home (www.bkjia.com) Tutorial recently implements the single-item selection function when it is working on a project. It can be implemented with RadioButtonList. Unfortunately, the selection of RadioButtonList cannot be canceled. Therefore, Javascript is used to cancel the selection.

First, define a global array variable to save the selection status.

Reference content is as follows:
// Save the selected status of each option
Var arrayChecked = new Array ();

Then, register the onload = "InitChecked ()" event and initialize the array. The purpose of this step is to ensure that the saved options are selected when they are loaded.

Reference content is as follows:
// Initialize the selection status
Function InitChecked (){
Var controlID = 'radioemprole1 ';
Var table = document. getElementById (controlID );
If (table ){
RadioButtonList = table. getElementsByTagName ('input ');
For (var I = 0; I <radioButtonList. length; I ++ ){
If (radioButtonList. item (I). checked ){
RadioButtonList [I] = 1; // selected
}
Else {
RadioButtonList [I] = 0; // not selected
}
}
}
}

Finally, register the onclick = "Checked ()" event for the RadioButtonList. When you select the option, you can determine whether the option is deselected Based on the option status and historical status.

Reference content is as follows:
// Set the selection status
Function Checked (){
Var controlID = 'radioemprole1 ';
Var table = document. getElementById (controlID );
If (table ){
RadioButtonList = table. getElementsByTagName ('input ');
For (var I = 0; I <radioButtonList. length; I ++ ){
If (radioButtonList. item (I). checked) {// when this option is selected, the Historical Status of this option is determined. if this option is also selected, the selection is canceled.
If (arrayChecked [I] = 1 ){
RadioButtonList. item (I). checked = false;
ArrayChecked [I] = 0;
}
Else {
ArrayChecked [I] = 1;
}
}
Else {
ArrayChecked [I] = 0;
}
}
}
}

You can click the selected option in the preceding three steps to cancel the selection.

Related Article

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.