我需要找到单选按钮,文本和选择的输入类型。使用< 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
原文地址:https://www.jb51.cc/jquery/181791.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。