jQuery find input type (but also for select)

后端 未结 2 1880
無奈伤痛
無奈伤痛 2021-02-03 21:31

I need to find the input type for radio buttons, text, and selects. Its easy to find the input type of anything with since $(this).at

相关标签:
2条回答
  • 2021-02-03 21:55

    You can do this (fiddle here), make some sort of easy to use plugin:

    $.fn.getType = function(){ return this[0].tagName == "INPUT" ? this[0].type.toLowerCase() : this[0].tagName.toLowerCase(); }
    

    And use it like this

    $(".element").getType(); // Will return radio, text, checkbox, select, textarea, etc (also DIV, SPAN, all element types)
    $(".elList").getType(); // Gets the first element's type
    

    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:

    $("input:text, input:radio, select");
    

    Or select all form controls (more info):

    $(":input") // can return all form types
    
    0 讨论(0)
  • 2021-02-03 22:16

    All types of input box and select box getting together by jQuery find :

    $('#myForm').find('select,input').each(function(i,box){
         alert($(box).attr('name'));
    }
    
    0 讨论(0)
提交回复
热议问题