How do I determine an HTML input element type upon an event using jQuery?

后端 未结 5 2066
逝去的感伤
逝去的感伤 2020-12-21 07:22

Given the following sample code:

$(document).ready(function(){
    $(\":input\").blur(function(){
        alert(\"The input type is:\" );  //How would this l         


        
相关标签:
5条回答
  • 2020-12-21 07:31
    $(this).attr("type");
    

    for example:

    $(document).ready(function(){
        $("input").blur(function(){
            alert("The input type is:" + $(this).attr("type"));
        })
    });
    
    0 讨论(0)
  • 2020-12-21 07:39
    $(this).attr("type");
    

    See jQuery's Selectors/Attribute documentation for additional information.

    0 讨论(0)
  • 2020-12-21 07:39

    Why not go through and see what attribute/property would be most useful?

    $(document).ready(function(){
        $("input").blur(function(){
            for (var x in this)
                alert(x + ":" + this[x]);
        })
    });
    
    0 讨论(0)
  • 2020-12-21 07:42

    How can I deteminedetermine whether this is an input, select, text, etc?

    Note that select, textarea, "etc" elements are not covered by $('input'). You probably rather want to use $(':input') to get them all.

    $(document).ready(function(){
        $(':input').blur(function(){
            alert('The tag is:' + this.tagName);
            if (this.tagName == 'INPUT') {
               alert("The input type is:" + $(this).attr('type'));
            }
        })
    });
    
    0 讨论(0)
  • 2020-12-21 07:45

    This should work...

    $(document).ready(function(){
        $("input").blur(function(){
            var type = this.type;
            alert("The input type is:" + type);
        })
    });
    
    0 讨论(0)
提交回复
热议问题