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.