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

时间:2009-12-21 14:40:31

标签: javascript jquery html

给出以下示例代码:

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

我如何确定这是输入,选择,文本等?

这不是一个现实世界的例子,但应该足以满足这个问题的目的

5 个答案:

答案 0 :(得分:11)

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

有关其他信息,请参阅jQuery的Selectors/Attribute文档。

答案 1 :(得分:5)

  

如何确定这是输入,选择,文本等?

请注意,select不涵盖textarea$('input'),“etc”元素。您可能更愿意使用$(':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'));
        }
    })
});

答案 2 :(得分:1)

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

例如:

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

答案 3 :(得分:0)

这应该有用......

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

答案 4 :(得分:0)

为什么不通过查看哪个属性/属性最有用?

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