如何测试收音机?

问题描述:

我有一个元素ID的数组,并希望检查元素的类型进一步处理。我的选择,文本,隐藏和密码测试工作正常,但不适用于收音机。这大概是我为每个使用。如何测试收音机?

if($('#elemID').is('select')) then do stuff... this works. 
if($('#elemID').is('input:text')) then do stuff... this works. 
if($('#elemID').is('input:hidden')) then do stuff... this works. 
if($('#elemID').is('input:password')) then do stuff... this works. 

if($('#elemID').is('input:radio')) then do stuff... this does **NOT** work. 

有关如何成功测试单选按钮元素ID为elemID的元素的任何想法将不胜感激。

+0

[:无线电](http://api.jquery.com/radio-selector/)它应该工作。 – Raynos 2011-04-01 16:05:15

+0

它应该工作..http://jsfiddle.net/sDXqF/ – 2011-04-01 16:11:28

It does work。你使用的是哪个版本的jQuery,你的标记是什么样的?这样做的

+0

jQuery v1.4.2 良好的调用标记...有一个名字,但没有ID ... Doh! TGIF。 – 2011-04-01 16:19:33

你可以简单地检查type属性,像这样:

var type = $('#elemID').attr('type'); //type will be either "hidden", "text", "radio", etc. 

一个当量的方法是

if($('#elemID').is('input[type=radio]')) 

也许这将更好地工作。如果它不起作用,我想看看你的阵列与元素