html
<input type ="radio" name="aa" id ="rod1"/>
<input type ="radio" name="aa" id ="rod2"/>
<input type ="radio" name="aa" id ="rod3"/>请问如何用js获取选中的id,并传给后台
<input type ="radio" name="aa" id ="rod1"/>
<input type ="radio" name="aa" id ="rod2"/>
<input type ="radio" name="aa" id ="rod3"/>请问如何用js获取选中的id,并传给后台
document.body.onclick = function(){
var els = document.getElementsByName("aa");
for (var i = els.length; i--;){
var el = els[i]
if (el.checked){
alert(el.getAttribute("id"))
}
}
}
<input type ="radio" name="aa" id ="rod1" value="rod1"/>
<input type ="radio" name="aa" id ="rod2" value="rod1"/>
<input type ="radio" name="aa" id ="rod3" value="rod1"/>
function GetSelectedVal()
{
var rds=document.getElementsByName("aa");
var rdVal;
for(var i=0;i<rds.length;i++)
{
if(rds.item(i).checked){
rdVal=rds.item(i).getAttribute("value");
alert(rdVal);
break;
}
else{
continue;
}
}
}
<input type ="radio" name="aa" id ="rod2" onclick="fn(this);"/>2
<input type ="radio" name="aa" id ="rod3" onclick="fn(this);"/>3<script>
function fn(_self){
if(_self.checked) alert(_self.id);
}
</script>
<input type ="radio" name="aa" id ="rod1"/>
<input type ="radio" name="aa" id ="rod2"/>
<input type ="radio" name="aa" id ="rod3"/>
<input type="hidden" name="radioValue" id="radioValue">
<input type="submit" onclick="fn()">加一个隐藏的input,将被选择的radio中的ID值赋给它,然后在后台取radioValue的值
function fn(){
var els = document.getElementsByName("aa"),
hEl = document.getElementById("radioValue");
for (var i = els.length; i--;){
var el = els[i];
if (el.checked){
hEl.value = el.getAttribute("id");
break;
}
}
}
后台用request.getParameter()取就行