JS obtains the code of the specified radio instance.
Use js to obtain the selected radio value, which is often used on the submission page. It also introduces how to obtain the radio value without selecting it. Example,
-
-
- Text
- Script
- Var chk = 0;
- Window. onload = function (){
- Var chkObjs = document. getElementsByName ("radio ");
- For (var I = 0; I If (chkObjs [I]. checked ){
- Chk = I;
- Break;
- }
- }
- }
- Function check_radio (){
- Var chkObjs = document. getElementsByName ("radio ");
- For (var I = 0; I If (chkObjs [I]. checked ){
- If (chk = I ){
- Alert ("cannot submit if the radio value is not changed ");
- Break;
- }
- }
- }
- }
- Script
-
-
-
-
Do not select to obtain the radio value
- USD
- Yen
- Euro
Native JS method: (native DOM operations also regard text as a node, so there will be nextSibling)
- Var USD = document. getElementsByName ("money") [0]. nextSibling. nodeValue;
- Var yen = document. getElementsByName ("money") [1]. nextSibling. nodeValue;
- Var euro = document. getElementsByName ("money") [2]. nextSibling. nodeValue;
Obtain the radio value using jQuery.
$ ('Input [name = "money"]: checked'{.next('span'{.html ();
USD
- Yen
- Euro
- USD
Which of the following is to be selected.
Function radioValue (){
- Var radArr = document. getElementsByName ("radiov ");
- Var radValue = "";
- // Alert (radArr. length );
- For (var I = 0; I // Alert (radArr [I]. checked + "" + radArr [I]. name + "" + radArr [I]. value );
- If (radArr [I]. checked ){
- RadValue = radArr [I]. value;
- }
- }
If (radValue! = Null & radValue! = ""){
- Alert (radValue );
- } Else {
- Alert ("Select ");
- }
}
|