jQuery查找输入类型(也可以选择)

我需要找到单选按钮,文本和选择的输入类型。使用< input type =“x”>很容易找到任何东西的输入类型因为$(this).attr(“type”)将返回x

我的问题是我需要支持< select>元素,没有type属性。最终的目的是做回收收音机,文本或选择。

我想到这样做,但是如果有一个更好的方法,我很好奇:

if ($(this).tagName == "input") {
    var result = $(this).attr("type");   //returns radio or text (the attr type)
} else {
    var result = $(this).tagName;        //returns select (the element type)
}

感谢所有!

解决方法

你可以这样做( fiddle here),做一些易于使用的插件
$.fn.getType = function(){ return this[0].tagName == "INPUT" ? this[0].type.toLowerCase() : this[0].tagName.toLowerCase(); }

并使用它这样

$(".element").getType(); // Will return radio,text,checkBox,select,textarea,etc (also DIV,SPAN,all element types)
$(".elList").getType(); // Gets the first element's type

哪个将获得所选择的第一个元素的类型。

其他信息

如果你只想要一些选择器,你可以使用这个:

$("input:text,input:radio,select");

或选择所有表单控件(more info):

$(":input") // can return all form types

相关文章

页面搜索关键词突出 // 页面搜索关键词突出 $(function () {...
jQuery实时显示日期、时间 html: &lt;span id=&quot...
jQuery 添加水印 &lt;script src=&quot;../../../.....
中文:Sys.WebForms.PageRequestManagerParserErrorExceptio...
1. 用Response.Write方法 代码如下: Response.Write(&q...
Jquery实现按钮点击遮罩加载,处理完后恢复 思路: 1.点击按...