Use js's for loop to get the value selected by radio

  • 2020-03-26 21:33:46
  • OfStack

Such as:
 
<li><input type="radio" name="zt" value="1"></li> 
<li><input type="radio" checked="checked" name="zt" value="2"></li> 
<li><input type="radio" name="zt" value="3"></li> 

The value of the name attribute must be the same. Next, js is used to get the selected value:
 
var zt = document.getElementsByName("zt"); 

for(var i=0;i<zt.length;i++){ 
if(zt[i].checked) { 
alert(zt[i].value); 
} 
} 

I'm done with a for loop.

Related articles: