How to get input type using jquery?

后端 未结 11 1817
猫巷女王i
猫巷女王i 2020-11-28 02:17

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

11条回答
  •  爱一瞬间的悲伤
    2020-11-28 02:31

    var val = $('input:checkbox:checked, input:radio:checked, \
       select option:selected, textarea, input:text',
       $('#container')).val();
    

    Comments:

    1. I assume, that there is exactly one form element, that can be either a textarea, input field, select form, a set of radio buttons or a single checkbox (you will have to update my code if you need more checkboxes).

    2. The element in question lives inside an element with ID container (to remove ambiguences with other elements on the page).

    3. The code will then return the value of the first matching element it finds. Since I use :checked and so on, this should always be exactly the value of what you're looking for.

提交回复
热议问题