Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/457.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何检查JQuery变量的类型?_Javascript_Jquery - Fatal编程技术网

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

Javascript 如何检查JQuery变量的类型?,javascript,jquery,Javascript,Jquery,我试图在触发函数的输入不是submit类型的情况下执行函数 $(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 tha

我试图在触发函数的输入不是submit类型的情况下执行函数

$(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类型的输入。当类型为submit的输入触发外部函数运行时,我希望此代码触发第一个条件语句

将attr方法与下面的“type”一起使用

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

}

将attr方法与“type”一起使用,如下所示

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

}

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

if (this.type === 'submit') {
    // ...
} else {
    // ...
}
(至少直到涉及另一个
函数
,因为它将有自己的
值。)

您当前的条件是测试字符串的属性,
“input”
,它可能是
未定义的
,并且不等于
“submit”


不过,如果您只想完全排除提交按钮,还可以使用选择器和jQuery的自定义选择器来实现这一点

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

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

if (this.type === 'submit') {
    // ...
} else {
    // ...
}
(至少直到涉及另一个
函数
,因为它将有自己的
值。)

您当前的条件是测试字符串的属性,
“input”
,它可能是
未定义的
,并且不等于
“submit”


不过,如果您只想完全排除提交按钮,还可以使用选择器和jQuery的自定义选择器来实现这一点

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

如果有人将其作为html提交,那么这将不起作用,而这不是提交输入类型。。。或者更好地解释:)@VtoCorleone您的示例没有提交类型,这就是问题所问的for@aw04-没有指定提交类型的chrome默认按钮。不确定其他浏览器。无论哪种方式,都不能保证指定类型。@VtoCorleone在这种情况下,所使用的选择器已经排除了
s。但是,是的,默认情况下,它的
type
属性将是
submit
,而属性可以为空。如果有人将其作为html提交,则这将不起作用,而这不是提交输入类型。。。或者更好地解释:)@VtoCorleone您的示例没有提交类型,这就是问题所问的for@aw04-没有指定提交类型的chrome默认按钮。不确定其他浏览器。无论哪种方式,都不能保证指定类型。@VtoCorleone在这种情况下,所使用的选择器已经排除了
s。但是,是的,它的
type
属性默认为
submit
,而属性可以为空。我喜欢第二个选项,更优雅=)我喜欢第二个选项,更优雅=)