怎么用JS获取单选按钮的值,
展开全部
function getSelectedText (name){
var obj=document.getElementByd(name);
for(i=0;i<obj.length;i++){
if(obj[i].selected==true){
return obj[i].innerText;
}
}
}
function getSelectedValue(name){
var obj=document.getElementById(name);
return obj.value;
}
var obj=document.getElementByd(name);
for(i=0;i<obj.length;i++){
if(obj[i].selected==true){
return obj[i].innerText;
}
}
}
function getSelectedValue(name){
var obj=document.getElementById(name);
return obj.value;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
<input type="radio" name="picno" value="1" id="pocno" />
<script>
var s =document.getElementById('pocno').value;
alert(s);
</script>
<script>
var s =document.getElementById('pocno').value;
alert(s);
</script>
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
测试代码radio.html 如下:
=============================================================================
<html>
<head></head>
<script language="javascript">
function getRadio(oRadio){
var oRadioLength= oRadio.length;
var oRadioValue = false;
//alert("oRadioLength:["+oRadioLength+"]");
if (oRadioLength== undefined){
if (oRadio.checked){
oRadioValue = oRadio.value;
}
}else{
for (i=0;i<oRadioLength;i++){
//alert("oRadio["+i+"]:"+oRadio[i].checked+"/"+oRadio[i].value);
if (oRadio[i].checked){
oRadioValue = oRadio[i].value;
break;
}
}
}
return oRadioValue;
}
//方法改进:
//求单选按纽的值,传radio名字作为参数。未选返回false;有选择项,返回选项值。
function getRadioValue(name){
var radioes = document.getElementsByName(name);
for(var i=0;i<radioes.length;i++)
{
if(radioes[i].checked){
return radioes[i].value;
}
}
return false;
}
//通过值修改所选中的单选按钮
function changeRadio(oRadio,oRadioValue){ //传入一个对象
for(var i=0;i<oRadio.length;i++) //循环
{
if(oRadio[i].value==oRadioValue) //比较值
{
oRadio[i].checked=true; //修改选中状态
break; //停止循环
}
}
}
//改进:
//通过值修改所选中的单选按钮
function setRadio(name,sRadioValue){ //传入radio的name和选中项的值
var oRadio = document.getElementsByName(name);
for(var i=0;i<oRadio.length;i++) //循环
{
if(oRadio[i].value==sRadioValue) //比较值
{
oRadio[i].checked=true; //修改选中状态
break; //停止循环
}
}
}
</script>
<body>
<form name="frm">
<input type="radio" name="oper" value="agree"
>同意</td>
<input type="radio" name="oper" value="downchange"
checked>下发修改</td>
<input type="radio" name="oper"
value="refuse">拒保</td>
<input type="radio" name="oper" value="report"
>上报 </td>
<br>
alert('result:'+getRadio(this.form.oper))
<input type="button" name="test1" value="按钮1"
onclick="alert('result:'+getRadio(this.form.oper));">
<br>
alert('result:'+getRadio(document.getElementByIdx_x('oper')))
<input type="button" name="test2" value="按钮2"
onclick="alert('result:'+getRadio(document.getElementByIdx_x('oper')));">
<br>
alert(this.form.oper.value)
<input type="button" name="test3" value="按钮3"
onclick="alert(this.form.oper.value);">
<br>
changeRadio(this.form.oper,"上报")
<input type="button" name="test4" value="按钮4"
onclick="changeRadio(this.form.oper,'report');">
<br><br><br><br>
<select id="slt">
<option value="agree"
>同意</option>
<option value="downchange"
selected>下发修改</option>
<option
value="refuse">拒保</option>
<option
value="report">上报</option>
</select>
<br>
alert(this.form.slt.value)
<input type="button" name="test5" value="按钮5"
onclick="alert(this.form.slt.value);">
<br>
document.getElementByIdx_x('slt')[2].innerText)
<input type="button" name="test6" value="按钮6"
onclick="alert(document.getElementByIdx_x('slt')[2].innerText);">
</form>
</body>
=============================================================================
<html>
<head></head>
<script language="javascript">
function getRadio(oRadio){
var oRadioLength= oRadio.length;
var oRadioValue = false;
//alert("oRadioLength:["+oRadioLength+"]");
if (oRadioLength== undefined){
if (oRadio.checked){
oRadioValue = oRadio.value;
}
}else{
for (i=0;i<oRadioLength;i++){
//alert("oRadio["+i+"]:"+oRadio[i].checked+"/"+oRadio[i].value);
if (oRadio[i].checked){
oRadioValue = oRadio[i].value;
break;
}
}
}
return oRadioValue;
}
//方法改进:
//求单选按纽的值,传radio名字作为参数。未选返回false;有选择项,返回选项值。
function getRadioValue(name){
var radioes = document.getElementsByName(name);
for(var i=0;i<radioes.length;i++)
{
if(radioes[i].checked){
return radioes[i].value;
}
}
return false;
}
//通过值修改所选中的单选按钮
function changeRadio(oRadio,oRadioValue){ //传入一个对象
for(var i=0;i<oRadio.length;i++) //循环
{
if(oRadio[i].value==oRadioValue) //比较值
{
oRadio[i].checked=true; //修改选中状态
break; //停止循环
}
}
}
//改进:
//通过值修改所选中的单选按钮
function setRadio(name,sRadioValue){ //传入radio的name和选中项的值
var oRadio = document.getElementsByName(name);
for(var i=0;i<oRadio.length;i++) //循环
{
if(oRadio[i].value==sRadioValue) //比较值
{
oRadio[i].checked=true; //修改选中状态
break; //停止循环
}
}
}
</script>
<body>
<form name="frm">
<input type="radio" name="oper" value="agree"
>同意</td>
<input type="radio" name="oper" value="downchange"
checked>下发修改</td>
<input type="radio" name="oper"
value="refuse">拒保</td>
<input type="radio" name="oper" value="report"
>上报 </td>
<br>
alert('result:'+getRadio(this.form.oper))
<input type="button" name="test1" value="按钮1"
onclick="alert('result:'+getRadio(this.form.oper));">
<br>
alert('result:'+getRadio(document.getElementByIdx_x('oper')))
<input type="button" name="test2" value="按钮2"
onclick="alert('result:'+getRadio(document.getElementByIdx_x('oper')));">
<br>
alert(this.form.oper.value)
<input type="button" name="test3" value="按钮3"
onclick="alert(this.form.oper.value);">
<br>
changeRadio(this.form.oper,"上报")
<input type="button" name="test4" value="按钮4"
onclick="changeRadio(this.form.oper,'report');">
<br><br><br><br>
<select id="slt">
<option value="agree"
>同意</option>
<option value="downchange"
selected>下发修改</option>
<option
value="refuse">拒保</option>
<option
value="report">上报</option>
</select>
<br>
alert(this.form.slt.value)
<input type="button" name="test5" value="按钮5"
onclick="alert(this.form.slt.value);">
<br>
document.getElementByIdx_x('slt')[2].innerText)
<input type="button" name="test6" value="按钮6"
onclick="alert(document.getElementByIdx_x('slt')[2].innerText);">
</form>
</body>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询