开发者

How do you test whether a field is of type "select" in jQuery

How do you test an input field for typ开发者_运维技巧e "select", e.g. for a radio button, it's

if($('#' + field).attr('type') == 'radio'{
....
}

What do you write for a select box?


You can use .is() and an element selector, like this:

if($('#' + field).is('select')) {

Or check .length of an #id selector only for that element type like this (a bit slower):

if($('select#' + field).length) {

Or nodeName (or tagName) like this:

if($('#' + field)[0].tagName == 'SELECT') {

Or without jQuery at all (fastest):

if(document.getElementById(field).tagName == 'SELECT') {
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜