jQuery 查找输入类型(也适用于选择)
我需要找到单选按钮、文本和选择的输入类型。使用 可以轻松找到任何内容的输入类型,因为
$(this).attr("type")
将返回 x
我的问题是我需要支持
我想做这样的事情,但我很好奇是否有更好的方法:
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)
}
谢谢大家!
I need to find the input type for radio buttons, text, and selects. Its easy to find the input type of anything with <input type="x">
since $(this).attr("type")
would return x
My issue is that I need to support <select>
elements, which dont have the type attribute. The end goal is to do return either radio, text, or select.
I thought of doing something like this, but I was curious if there's a better way:
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)
}
Thanks all!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你可以这样做(fiddle here),制作某种易于使用的插件:
并像这样使用它这
将获取选定的第一个元素的类型。
其他信息
如果您只想拥有一些选择器,您可以使用此:
或选择所有表单控件(更多信息):
You can do this (fiddle here), make some sort of easy to use plugin:
And use it like this
Which will get the type of the first element which is selected.
Other info
If you just want to have some selectors you can use this:
Or select all form controls (more info):
所有类型的输入框和选择框通过 jQuery find 组合在一起:
All types of input box and select box getting together by jQuery find :