Home > Web Front-end > JS Tutorial > JS How to get the value of the radio after it is selected and the value of the radio when it is not selected_javascript skills

JS How to get the value of the radio after it is selected and the value of the radio when it is not selected_javascript skills

WBOY
Release: 2016-05-16 17:18:39
Original
1515 people have browsed it

The following is an article taken from the Internet (not tested but very standard and can be imitated)

Copy the code The code is as follows:




text
<script> <br>var chk = 0; <br>window.onload=function (){ <br>var chkObjs = document.getElementsByName("radio"); <br>for (var i=0;i<chkObjs.length;i ){ <BR>if(chkObjs[i].checked){ <BR>chk = i; <BR>break; <BR>} <BR>} <BR>} <BR>function check_radio(){ <BR>var chkObjs = document.getElementsByName("radio"); <BR>for(var i=0;i<chkObjs.length;i ){ <BR>if(chkObjs [i].checked){ <BR>if(chk == i){ <BR>alert("radio value has not changed and cannot be submitted"); <BR>break; <BR>} <BR>} <BR>} <BR>} <BR></script>



一;
two;
three;
four;
five;





The following is to get the value of radio without making a selection
Copy code The code is as follows:

USD
JPY
EUR

Native JS method: (Native DOM operation will treat text as a node, so there will be nextSibling)
Copy Code The code is as follows:

var USD = document.getElementsByName("money")[0].nextSibling.nodeValue;
var Yen = document. getElementsByName("money")[1].nextSibling.nodeValue;
var euro = document.getElementsByName("money")[2].nextSibling.nodeValue;

jQuery way
Copy code The code is as follows:

$('input[name="money"]:checked') .next('span').html();

USD< ;/span>
yen
Euro




The following are to be selected:

This only determines whether there is a selection
Copy code The code is as follows:

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("Please select");
}

}

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template