简体   繁体   English

jQuery通过表单输入type =“ file”提交后检测插入的图像

[英]JQuery detect inserted image after submitting through form input type=“file”

I'm trying to detect an image submited through a form so I can proceed with the execution of some more code. 我试图检测通过表单提交的图像,以便继续执行更多代码。 The image is submited throw a form input type="file" and than rendered in the page throw ajax using jquery.form.js plugin. form input type="file"图像后,使用form input type="file"提交,然后使用jquery.form.js插件在页面中将其呈现为ajax。

I've tryed the on.load method but it seems to only work with images called in the file code. 我已经on.load方法,但是它似乎只适用于文件代码中调用的图像。

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

And: 和:

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

I've also tryed the plugin from https://github.com/desandro/imagesloaded but didn't work as I was hopping to work. 我也尝试过https://github.com/desandro/imagesloaded的插件,但是由于我希望工作而无法使用。

Is there a way to have something that can detect when a image is submitted, than loaded to the DOM of the page? 有什么方法可以检测到何时提交图像,而不是将其加载到页面的DOM中?

UPDATE 更新
upload handler 上传处理程序

$('.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');
    });
});

It's still somewhat unclear exactly what you're asking for but assuming that the file is successfully uploaded and an image tag is added to some container on the page, you should be able to detect when the new image has been loaded by delegating the load event to the container using the selector as a filter. 仍然不清楚您到底要什么,但假设文件已成功上传并且图像标签已添加到页面上的某个容器中,那么您应该能够通过委派load事件来检测何时已加载新图像使用选择器作为过滤器添加到容器中

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

Using delegation allows the handler to apply both to images loaded when the page is first rendered and elements that are dynamically added to the page later. 使用委托允许处理程序将其应用于第一次呈现页面时加载的图像以及以后动态添加到页面中的元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM