我需要找到单选按钮、文本和选择的输入类型。很容易找到任何东西的输入类型<input type="x">
since $(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)
}
谢谢大家!
你可以这样做 (在这里摆弄 http://jsfiddle.net/vm9Py/152/),制作一些易于使用的插件:
$.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");
或者选择所有表单控件(更多信息 http://api.jquery.com/input-selector/):
$(":input") // can return all form types
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)