jQuery - determine if input element is textbox or select list

后端 未结 3 1706
面向向阳花
面向向阳花 2020-12-24 11:08

How would I determine whether the element returned by an :input filter in jQuery is a textbox or select list?

I want to have a different behavior for each ( textbox

3条回答
  •  悲&欢浪女
    2020-12-24 12:07

    You could do this:

    if( ctrl[0].nodeName.toLowerCase() === 'input' ) {
        // it was an input
    }
    

    or this, which is slower, but shorter and cleaner:

    if( ctrl.is('input') ) {
        // it was an input
    }
    

    If you want to be more specific, you can test the type:

    if( ctrl.is('input:text') ) {
        // it was an input
    }
    

提交回复
热议问题