JavaScript 设置和获得Radio的选中值-转

function setCheckedValue(radioName, newValue) {   
    if(!radioName) return;   
       var radios = document.getElementsByName(radioName);      
       for(var i=0; i<radios.length; i++) {   
          radios[i].checked = false;   
          if(radios[i].value == newValue.toString()) {   
          radios[i].checked = true;   
       }   
        }   
}  

 

获得单选框选中值

 

function getRadioValue(radioName){//得到radio的值      
        var obj=document.getElementsByName(radioName);      
        for(var i=0;i<obj.length;i++){      
        if(obj[i].checked){      
            alert(obj[i].value);   
            return obj[i].value;      
            }      
        }      
    }   

 

你可能感兴趣的:(JavaScript)