如何检查我的JQuery变量的类型?

时间:2014-04-18 01:53:38

标签: javascript jquery

我正在尝试执行一个函数,条件是触发它的输入不是提交类型。

$(document).ready(function() {

//triggers the capture() function whenever any input on the page loses focus
$("input").blur(function() {

// This is the part that's not working. I need to check if the specific input that 
// triggered the code to run is of type submit. 

if (("input").type == 'submit') {
    alert("this worked");
}
else {

    capture();

}
});

});

现在正在调用capture(),即使我模糊了提交类型的输入。我希望这个代码在类型为submit的输入触发外部函数运行时触发第一个条件语句。

2 个答案:

答案 0 :(得分:6)

jQuery会将事件回调中的this值分配为触发事件的元素,因此您可以使用该属性访问其属性:

if (this.type === 'submit') {
    // ...
} else {
    // ...
}

(至少涉及到另一个function,因为它会有自己的this值。)

您目前的条件是测试字符串"input"的属性,该属性可能为undefined且不等于'submit'


但是,如果你只是想完全排除提交按钮,你也可以使用:not()和jQuery的自定义:submit选择器来使用选择器。

$('input:not(:submit)').blur(capture);

答案 1 :(得分:3)

像这样使用'type'的attr方法

if($(this).attr('type') === 'submit'){

}
相关问题