如何使用jQuery获取输入类型? [英] How to get input type using jquery?

查看:45
本文介绍了如何使用jQuery获取输入类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面,其中输入类型总是变化的,我需要获取取决于输入类型的值.因此,如果类型是单选,我需要获取选中的内容,如果是复选框,则现在需要选中的内容,如果是下拉列表,则需要知道选择了哪个,如果是一个文本/文本区域,我需要知道这些值.

I have a page where the input type always varies, and I need to get the values depending on the input type. So if the type is a radio, I need to get which is checked, and if it is a checkbox I need to now which are checked, and if it is a drop down I need to know which is selected, and I if it is a text/textarea I need to know the values.

关于如何做到这一点的任何想法?

Any idea on how to do that?

推荐答案

EDIT 2013年2月1日.由于此答案的普及以及1.9(和2.0)版本中有关属性和属性的jQuery更改,我添加了一些内容注释和小提琴,以查看访问输入,按钮和某些选择的属性/属性时它如何工作.这里的小提琴: http://jsfiddle.net/pVBU8/1/

EDIT Feb 1, 2013. Due to the popularity of this answer and the changes to jQuery in version 1.9 (and 2.0) regarding properties and attributes, I added some notes and a fiddle to see how it works when accessing properties/attributes on input, buttons and some selects. The fiddle here: http://jsfiddle.net/pVBU8/1/

获取所有输入:

var allInputs = $(":input");

获取所有输入类型:

allInputs.attr('type');

获取值:

allInputs.val();

注意:.val()与:check不同,这些类型是相关的. 使用:

NOTE: .val() is NOT the same as :checked for those types where that is relevent. use:

.attr("checked");


编辑2013年2月1日-重新:jQuery 1.9使用prop()而不是attr(),因为attr不会为已更改的属性返回正确的值.


EDIT Feb 1, 2013 - re: jQuery 1.9 use prop() not attr() as attr will not return proper values for properties that have changed.

.prop('checked');

或者简单地

$(this).checked;


获取支票的价值-无论当前是什么.或者如果只希望那些被选中的代码,则只需使用':checked'.


to get the value of the check - whatever it is currently. or simply use the ':checked' if you want only those that ARE checked.

这是获取类型的另一种方法:

Here is another way to get type:

var allCheckboxes=$('[type=checkbox]');

请注意以下形式:

$('input:radio');

被认为是

$(':radio');

都等于:

$('input[type=radio]');

,但是需要输入",因此当使用$(':radio')的形式等于$('*:radio');

but the "input" is desired so it only gets the inputs and does not use the universal '*" when the form of $(':radio') is used which equates to $('*:radio');

编辑2015年8月19日:应使用$('input[type=radio]');的首选项,因为这样可使现代浏览器优化对广播输入的搜索.

EDIT Aug 19, 2015: preference for the $('input[type=radio]'); should be used as that then allows modern browsers to optimize the search for a radio input.

EDIT 2013年2月1日,每则评论重新:选择元素 @dariomac

EDIT Feb 1, 2013 per comment re: select elements @dariomac

$('select').prop("type");

会根据"multiple"属性和

will return either "select-one" or "select-multiple" depending upon the "multiple" attribute and

$('select')[0].type 

如果存在

,则为第一个选择返回相同的内容.和

returns the same for the first select if it exists. and

($('select')[0]?$('select')[0].type:"howdy") 

将返回该类型(如果存在)或"howdy"(如果不存在).

will return the type if it exists or "howdy" if it does not.

 $('select').prop('type');

如果存在则返回DOM中第一个属性的属性,如果不存在则返回"undefined".

returns the property of the first one in the DOM if it exists or "undefined" if none exist.

$('select').type

返回第一个的类型(如果存在),或者返回错误(如果不存在).

returns the type of the first one if it exists or an error if none exist.

这篇关于如何使用jQuery获取输入类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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