JavaScript在IE和FireFox中读取单选按钮值 [英] JavaScript read radio button value in IE and FireFox

查看:192
本文介绍了JavaScript在IE和FireFox中读取单选按钮值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的使用JavaScript构建POST语句的Web表单。在Chrome中,我可以使用一行简单的代码...

  var form = document.forms ['myForm']; 
var env = form.env.value;

表单本身看起来像这样...

 < form name =myFormaction ='JavaScript:xmlhttpPost(/ path / to / some / pythoncode.py)'> 
< input type =radioname =envid =envvalue =inside> Inside
< input type =radioname =envid = envvalue =outsidechecked =checked>外
< ; input type =radioname =envid =envvalue =none>

我在窗体上有一些文本框,我可以使用相同的技术来查找值(
var name = form.fname.value
with a
< input type =textname =fnameid =fname>
但是,当我提交表单并构建我的文章,单选按钮的值总是未定义的。它在Chrome中正常工作,但在IE或FireFox中没有任何效果。



我试过 var env = document.getElementById('env')。value ,但出于某种原因,无论我选择什么,总是默认为第一个值(内部)。使用Chrome时返回一个值。

尝试阅读FF或IE中的广播输入的选中值时,是否缺少某些东西?试试

这个

 函数getValueFromRadioButton(name){
//获取名称为
的所有元素var buttons = document.getElementsByName(名称);
for(var i = 0; i< buttons.length; i ++){
//检查按钮是否被选中
var button = buttons [i];
if(button.checked){
//返回值
return button.value;
}
}
//没有选择单选按钮。
返回null;
}

ID是唯一的,所以您不应该为多个项目使用相同的ID。如果您使用此功能,您可以删除所有单选按钮ID。

I have a simple web form that uses JavaScript for building a POST statement. In Chrome, I can use a simple line of code...

var form = document.forms['myForm'];
var env = form.env.value;

The form itself looks like this...

<form name="myForm" action='JavaScript:xmlhttpPost("/path/to/some/pythoncode.py")'>
    <input type="radio" name="env" id="env" value="inside">Inside
    <input type="radio" name="env" id="env" value="outside" checked="checked">Outside
    <input type="radio" name="env" id="env" value="both">Both
    <input type="radio" name="env" id="env" value="neither">Neither

I have some text boxes on the form that I can use the same technique to find the value ( var name = form.fname.value with a <input type="text" name="fname" id="fname"> However, when I submit the form and build my post, the value for the radio buttons is always undefined. It works fine in Chrome, but nothing in IE or FireFox.

I tried var env = document.getElementById('env').value, but for some reason that always defaults to the first value (inside) no matter what I select. That method also does not return a value when using Chrome.

Is there something I'm missing for reading the checked value of a radio input in FF or IE?

解决方案

Try this

function getValueFromRadioButton(name) {
   //Get all elements with the name
   var buttons = document.getElementsByName(name);
   for(var i = 0; i < buttons.length; i++) {
      //Check if button is checked
      var button = buttons[i];
      if(button.checked) {
         //Return value
         return button.value;
      }
   }
   //No radio button is selected. 
   return null;
}

IDs are unique so you should not use the same ID for multiple items. You can remove the all the radio button IDs if you use this function.

这篇关于JavaScript在IE和FireFox中读取单选按钮值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆