检查输入是文本框,选择文本区域还是单选 [英] Check if input is textbox, select, textarea or radio

查看:71
本文介绍了检查输入是文本框,选择文本区域还是单选的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有不同html输入字段的表单...

I have a form with different html input fields...

1) <input type="text">
2) <textarea></textarea> 
3) <input type="checkbox">
4) <input type="radio">
5) <select></select>

如何使用jQuery确定输入字段的类型. 例如:如果我想检查输入是否为"select",则做一些事情.

How would I be able to determine what type of input field it is using jQuery. For example: If I wanted to do a check to see if input = "select" then do stuff.

推荐答案

$('input') // selects all types of inputs
$('input:checkbox') // selects checkboxes
$('select') // selects select element
$('input:radio') // selects radio inputs
$('input[type="text"]') // selects text inputs

您可以使用event.target.type,这会提醒事件的目标类型是inputtextreaselect.

you can use event.target.type, this alerts what type of input, textrea or select is the target of the event.

$('input, textarea, select').change(function(event){
   alert(event.target.type)
})

http://jsfiddle.net/Q4BNH/

这篇关于检查输入是文本框,选择文本区域还是单选的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆