如何确定使用jQuery的事件时的HTML输入元素类型?

问题描述:

考虑下面的示例代码:如何确定使用jQuery的事件时的HTML输入元素类型?

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

我怎样才能detemine这是否是一个输入,选择,文本等?

这不是一个真实的例子,但应该能满足这个问题

$(this).attr("type"); 

见jQuery的Selectors/Attribute文档的附加信息的目的。

$(this).attr("type"); 

例如:

$(document).ready(function(){ 
    $("input").blur(function(){ 
     alert("The input type is:" + $(this).attr("type")); 
    }) 
}); 

这应该工作...

$(document).ready(function(){ 
    $("input").blur(function(){ 
     var type = this.type; 
     alert("The input type is:" + type); 
    }) 
}); 
+0

为什么downvote? – Ryan

+1

+1当然这个工作,并且不浪费时间再次包装这个$ –

为什么不经历,看看有什么属性/属性将是最有用的?

$(document).ready(function(){ 
    $("input").blur(function(){ 
     for (var x in this) 
      alert(x + ":" + this[x]); 
    }) 
}); 

我怎样才能deteminedetermine这是否是一个输入,选择,文本等?

注意selecttextarea, “等” 的元素并不受$('input')。你可能宁愿使用$(':input')来获得全部。

$(document).ready(function(){ 
    $(':input').blur(function(){ 
     alert('The tag is:' + this.tagName); 
     if (this.tagName == 'INPUT') { 
      alert("The input type is:" + $(this).attr('type')); 
     } 
    }) 
});