How does JS get the selected value of the radio and not the selected value of the radio
- 2020-03-26 21:43:27
- OfStack
The following is an article taken off the web (untested but very standard to imitate)
The following is to get the value of the radio without making a choice
Native JS mode :(native DOM manipulation treats text as a node, so there will be nextSibling)
JQuery way
The following should be selected:
This one just determines if there is a choice
< Input type="button" value=" get data from test radio "onclick="radioValue();" / >
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>text</title>
<script>
var chk = 0;
window.onload=function (){
var chkObjs = document.getElementsByName("radio");
for(var i=0;i<chkObjs.length;i++){
if(chkObjs[i].checked){
chk = i;
break;
}
}
}
function check_radio(){
var chkObjs = document.getElementsByName("radio");
for(var i=0;i<chkObjs.length;i++){
if(chkObjs[i].checked){
if(chk == i){
alert("radio No change in value can be committed ");
break;
}
}
}
}
</script>
</head>
<body>
<form action='' method='post' onsubmit='javascript:return check_radio()'>
<input type='radio' value='1' name='radio' checked='checked'> one ;
<input type='radio' value='2' name='radio'> two ;
<input type='radio' value='3' name='radio'> three ;
<input type='radio' value='4' name='radio'> four ;
<input type='radio' value='5' name='radio'> five ;
<input type=submit value=sub >
</form>
</body>
</html>
The following is to get the value of the radio without making a choice
<input type="radio" name="money" value="1" /> The dollar
<input type="radio" name="money" value="2" /> The Japanese yen
<input type="radio" name="money" value="3" /> The euro
Native JS mode :(native DOM manipulation treats text as a node, so there will be nextSibling)
var The dollar = document.getElementsByName("money")[0].nextSibling.nodeValue;
var The Japanese yen = document.getElementsByName("money")[1].nextSibling.nodeValue;
var The euro = document.getElementsByName("money")[2].nextSibling.nodeValue;
JQuery way
$('input[name="money"]:checked').next('span').html();
<input type="radio" name="money" value="1" checked="checked" /><span> The dollar </span>
<input type="radio" name="money" value="2" /><span> The Japanese yen </span>
<input type="radio" name="money" value="3" /><span> The euro </span>
<!-- Normally, creating checkboxes or checkboxes is used label Link it, for example: -->
<input id="radio1" type="radio" name="money" value="1" /><label for="radio1"> The dollar </label>
The following should be selected:
This one just determines if there is a choice
function radioValue(){
var radArr = document.getElementsByName("radiov");
var radValue = "";
//alert(radArr.length);
for(var i=0; i<radArr.length; 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 a ");
}
}
< Input type="button" value=" get data from test radio "onclick="radioValue();" / >