How to get input type using jquery?

后端 未结 11 1803
猫巷女王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:39
      <!DOCTYPE html>
        <html lang="en">
        <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <meta http-equiv="X-UA-Compatible" content="ie=edge">
            <script type="text/javascript" src="hs/jquery1.js"></script>
            <title>Document</title>
        </head>
        <body>
            <form>
                <input type="text" class="tamil">
                <input type="button" class="english">
            </form>
            <script>
                $("input").addClass(function(index){
                        console.log($(this).attr('type'));
                })
            </script>
        </body>
        </html>
    

    i get wright form type

    0 讨论(0)
  • 2020-11-28 02:41

    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/


    get all the inputs:

    var allInputs = $(":input");
    

    get all the inputs type:

    allInputs.attr('type');
    

    get the values:

    allInputs.val();
    

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

    .attr("checked");
    

    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');
    

    or simply

    $(this).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.

    EDIT: Here is another way to get type:

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

    EDIT2: Note that the form of:

    $('input:radio');
    

    is perferred over

    $(':radio');
    

    which both equate to:

    $('input[type=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');

    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 Feb 1, 2013 per comment re: select elements @dariomac

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

    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") 
    

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

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

    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.

    0 讨论(0)
  • 2020-11-28 02:49

    It would seem that the attr functionality is getting further deprecated

    $(this).attr('TYPE')
    undefined
    $(this).prop('type')
    "text"
    
    0 讨论(0)
  • 2020-11-28 02:53

    In my application I ended up with two different elements having the same id (bad). One element was a div and the other an input. I was trying to sum up my inputs and took me a while to realise the duplicate ids. By placing the type of the element I wanted in front of #, I was able to grab the value of the input element and discard the div:

    $("input#_myelementid").val();
    

    I hope it helps.

    0 讨论(0)
  • 2020-11-28 02:55
    $("#yourobj").attr('type');
    
    0 讨论(0)
提交回复
热议问题