Get attribute name value of <input>

后端 未结 11 757
青春惊慌失措
青春惊慌失措 2020-11-29 20:11

How to get the attribute name value of a input tag using jQuery. Please help.


相关标签:
11条回答
  • 2020-11-29 20:40

    using value get input name

     $('input[value="1"]').attr('name');
    

    using id get input name

     $('input[class="id"]').attr('name');
       $('#id').attr('name');
    

    using class get input name

     $('input[value="classname"]').attr('name');
       $('.classname').attr('name');  //if classname have unique value
    
    0 讨论(0)
  • 2020-11-29 20:40

    For the below line, Initially faced problem with out giving single code that 'currnetRowAppName'value is not taking space with string. So, after that giving single code '"+row.applicationName+"', its taking space also.

    Example:

    <button class='btn btn-primary btn-xs appDelete' type='button' currnetRowAppName='"+row.applicationName+"' id="+row.id+ >
    
    var viewAppNAME = $(this).attr("currnetRowAppName");
    alert(viewAppNAME)
    

    This is working fine.

    0 讨论(0)
  • 2020-11-29 20:47

    If you're dealing with a single element preferably you should use the id selector as stated on GenericTypeTea answer and get the name like $("#id").attr("name");.

    But if you want, as I did when I found this question, a list with all the input names of a specific class (in my example a list with the names of all unchecked checkboxes with .selectable-checkbox class) you could do something like:

    var listOfUnchecked = $('.selectable-checkbox:unchecked').toArray().map(x=>x.name);
    

    or

    var listOfUnchecked = [];
    $('.selectable-checkbox:unchecked').each(function () {
        listOfUnchecked.push($(this).attr('name'));
    });
    
    0 讨论(0)
  • 2020-11-29 20:49
    var theName;
    
    theName = $("input selector goes here").attr("name");
    
    0 讨论(0)
  • 2020-11-29 20:50

    Pass class to input tag and access the name value by using class.

    <input class="test-class" name='xxxxx' value=1>
    
    <script>
        $('.test-class').attr('name');
    </script>
    

    Or you can also access the value using id.

    <input id="test-id" name='xxxxx' value=1>
    
    <script>
        $('#test-id').attr('name');
    </script>
    
    0 讨论(0)
提交回复
热议问题