JQuery 通过表单输入类型=“文件”提交后检测插入的图像


JQuery detect inserted image after submitting through form input type="file"

我正在尝试检测通过表单提交的图像,以便我可以继续执行更多代码。提交的图像抛出一个form input type="file",然后使用 jquery.form.js 插件在页面中呈现 ajax。

我已经尝试了on.load方法,但它似乎仅适用于文件代码中调用的图像。

$('.img_set').on('loaded', function (){
    alert();
});

和:

$(".img_set").on('load', function() {
  alert('I loaded!');
}).each(function() {
  if(this.complete) $(this).load();
});

我也尝试过 https://github.com/desandro/imagesloaded 的插件,但在我跳上班时不起作用。

有没有办法让一些东西可以检测何时提交图像,而不是加载到页面的 DOM 中?

更新
上传处理程序

$('.photoimg').on('change', function (){
    $('.db_result').html('<img src="images/loader.gif" />'); 
    $('.imageform').ajaxForm({ target: $(this).closest('.child')}).submit();
    $('.db_result').delay(500).queue(function(n) {
            $(this).html('');
    });
    $('.child').on('load','.img_set', function() {
        alert('new image loaded');
    });
});

目前还不清楚您确切的要求,但假设文件已成功上传并将图像标记添加到页面上的某个容器中,您应该能够通过使用选择器作为过滤器将 load 事件委托给容器来检测何时加载新图像。

$('#img_container').on('load','.img_set', function() {
    alert('new image loaded');
});

使用委派允许处理程序同时应用于首次呈现页面时加载的图像以及稍后动态添加到页面的元素。