Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/370.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 Image load()事件返回false不工作?_Javascript_Jquery - Fatal编程技术网

Javascript jQuery Image load()事件返回false不工作?

Javascript jQuery Image load()事件返回false不工作?,javascript,jquery,Javascript,Jquery,我写了一个函数来检查图像的高度和宽度。你可以在下面找到我的代码。它可以工作,但我遇到了一个问题,返回false:它不工作 $("#published").click(function( event ){ var img = $('<img src="'+selectedImage+'"/>').load(function( event ){ var orgWidth = this.width; var

我写了一个函数来检查图像的高度和宽度。你可以在下面找到我的代码。它可以工作,但我遇到了一个问题,返回false:它不工作

$("#published").click(function( event ){
    var img = $('<img src="'+selectedImage+'"/>').load(function( event  ){
                  var orgWidth = this.width;
                  var orgHeight = this.height;
                  console.log(orgWidth+" = "+orgHeight);

                  if(orgWidth >= 169 && orgHeight >= 169){
                    thisValidate.parent('div.rwmb-input').children('.imageError').remove();
                    return true;
                  }else{
                    event.preventDefault();
                    if(thisValidate.parent('div.rwmb-input').children('.imageError').length == 0){
                      thisValidate.parent('div.rwmb-input').append("<p class='imageError'></p>");
                    }
                    thisValidate.parent('div.rwmb-input').children('.imageError').html('<br/><label for="techranger_booked_feature_images" class="error">Upload Image More then 169 x 169</label>');
                    return false;
                  }
              });

});
$(“#已发布”)。单击(函数(事件){
var img=$('').load(函数(事件){
var orgWidth=此.width;
var orgHeight=此高度;
console.log(orgWidth+“=”+orgHeight);
如果(组织宽度>=169和组织高度>=169){
thisValidate.parent('div.rwmb-input').children('.imageError').remove();
返回true;
}否则{
event.preventDefault();
if(thisValidate.parent('div.rwmb-input').children('.imageError')。长度==0){
thisValidate.parent('div.rwmb-input')。追加(“

”; } thisValidate.parent('div.rwmb-input').children('.imageError').html('
上传图像超过169 x 169'); 返回false; } }); });

我添加了
event.preventDefault()但它也没有帮助。

您将返回到
load()
函数,该函数不会返回到外部单击函数

另外,
load()
是异步的,因此单击事件将在
load()
之前很久完成

最后,您有2个
事件
参数,而
加载
中的参数不会对单击产生任何影响

load()
成功时,您需要完全阻止单击并手动触发下一个事件

$("#published").click(function( event ){
     event.preventDefault();// prevent default
    // no access to "this" inside load() so store reference to form
    var $form = $(this).closest('form');

    var img = $('<img src="'+selectedImage+'"/>').load(function(imgEvt ){
           // code removed for clarity
          if(orgWidth >= 169 && orgHeight >= 169){
               /* trigger form submit */
               $form.submit();
          }else{
             // other code
          }
    });
});
$(“#已发布”)。单击(函数(事件){
event.preventDefault();//防止默认值
//无法访问load()中的“this”,因此请存储对表单的引用
var$form=$(this).closest('form');
var img=$('').load(函数(imgEvt){
//为清晰起见,代码已删除
如果(组织宽度>=169和组织高度>=169){
/*触发表单提交*/
$form.submit();
}否则{
//其他代码
}
});
});

您将返回到
load()
函数,该函数不会返回到外部单击函数

另外,
load()
是异步的,因此单击事件将在
load()
之前很久完成

最后,您有2个
事件
参数,而
加载
中的参数不会对单击产生任何影响

load()
成功时,您需要完全阻止单击并手动触发下一个事件

$("#published").click(function( event ){
     event.preventDefault();// prevent default
    // no access to "this" inside load() so store reference to form
    var $form = $(this).closest('form');

    var img = $('<img src="'+selectedImage+'"/>').load(function(imgEvt ){
           // code removed for clarity
          if(orgWidth >= 169 && orgHeight >= 169){
               /* trigger form submit */
               $form.submit();
          }else{
             // other code
          }
    });
});
$(“#已发布”)。单击(函数(事件){
event.preventDefault();//防止默认值
//无法访问load()中的“this”,因此请存储对表单的引用
var$form=$(this).closest('form');
var img=$('').load(函数(imgEvt){
//为清晰起见,代码已删除
如果(组织宽度>=169和组织高度>=169){
/*触发表单提交*/
$form.submit();
}否则{
//其他代码
}
});
});

首先,您的
返回false
正在为
加载
事件提供服务的函数范围内运行,您的
事件
jQuery
加载
函数的事件

其次,如果您想在按钮上执行此操作,则无法像您尝试的那样同步执行此操作,因为获取图像的请求是异步的,因此它发生在按钮事件上的函数完成后(因此您无法停止事件默认行为,因为它已经发生)。无论图像大小如何,都必须在
单击
方法中防止/返回false,然后在检查(un-?)成功时调用另一个函数。这是唯一的办法

$("#published").click(function(event){
  // here your check, e.g. if (imageFits) goOnFunction();

  event.preventDefault();
});
最后,你:


首先,您的
返回false
正在为
加载
事件提供服务的函数范围内运行,您的
事件
jQuery
加载
函数的事件

其次,如果您想在按钮上执行此操作,则无法像您尝试的那样同步执行此操作,因为获取图像的请求是异步的,因此它发生在按钮事件上的函数完成后(因此您无法停止事件默认行为,因为它已经发生)。无论图像大小如何,都必须在
单击
方法中防止/返回false,然后在检查(un-?)成功时调用另一个函数。这是唯一的办法

$("#published").click(function(event){
  // here your check, e.g. if (imageFits) goOnFunction();

  event.preventDefault();
});
最后,你:


我没有表格。我正在使用第三方表单谢谢你的回答好的,那么点击做什么呢?你必须截取它来做你想做的事情,或者检查图像元素,我在这里得到提交按钮的点击事件,比如
$(“输入[type='submit'])。点击(function(){//code})
确定。。。所以表单是否是第三方并不重要,除非按钮上也有第三方事件处理程序,我没有表单。我正在使用第三方表单谢谢你的回答好的,那么点击做什么呢?你必须截取它来做你想做的事情,或者检查图像元素,我在这里得到提交按钮的点击事件,比如
$(“输入[type='submit'])。点击(function(){//code})
确定。。。所以表单是否是第三方并不重要,除非按钮上也有第三方事件处理程序