简体   繁体   English

单击时替换动态生成的图像

[英]Replacing a dynamically generated images on click

How do I replace a dynamic image that is generated by a loop? 如何替换由循环生成的动态图像?

在此处输入图片说明

The above image should look like this in html rendered by mustache templating system. 上面的图像在由mustache模板系统呈现的html中应该看起来像这样。

{{#images}}

<div class="picture-caption">
    <img id="{{id}}" class="caption" src="./data/picture_caption/{{picture_caption}}" alt="your image" />
    <input id="image-value" data-id="{{id}}" class="image-box-{{id}}" type="file" style="display: none;" value="
{{picture_caption}}" />{{id}}
</div>

{{/answers}}

I did coding and worked out only changing for the first index in the loop (which is the son guko image) 我做了编码,只计算出循环中的第一个索引(这是儿子guko图像)

function readURL(input, caption_id) {
    if (input.files && input.files[0]) {
        var reader = new FileReader();

        reader.onload = function (e) {
            $("#"+caption_id).attr('src', e.target.result);
        }
        reader.readAsDataURL(input.files[0]);
    }
}

$(".caption").click(function(e) {
    var caption_id = $(this).attr('id');
    // console.log(caption_id);
    $(".image-box-"+caption_id).click();
});

$("#image-value").change(function(e){
    var caption_id = $(this).attr('data-id');
    // alert(caption_id);
    readURL(this, caption_id);
});

Any ideas/alternatives I can do to implement this? 我有什么想法/替代方法可以实现呢?

You have multiple input elements with same id ( id="image-value" ) that's why jQuery picks up only the first of them when executing onchange handler you binded here - $("#image-value").change(function(e){...}) . 您有多个具有相同id( id="image-value" )的输入元素,这就是为什么jQuery在执行您在此处绑定的onchange处理程序时仅选择第一个输入元素- $("#image-value").change(function(e){...})

So replace $("#image-value").change(function(e){...}) to $('[type="file"]').change(function(e){...}) like below to attach event handler to all input elements. 因此,将$("#image-value").change(function(e){...})替换$("#image-value").change(function(e){...}) $('[type="file"]').change(function(e){...})如下所示,将事件处理程序附加到所有输入元素。

Check demo - Fiddle . 检查演示Fiddle

function readURL(input, caption_id) {
    if (input.files && input.files[0]) {
        var reader = new FileReader();
        reader.onload = function (e) {
            $("#"+caption_id).attr('src', e.target.result);
        }
        reader.readAsDataURL(input.files[0]);
    }
}

$(".caption").click(function() {
    $(".image-box-"+this.id).click();
});

$('[type="file"]').change(function(e){
    var caption_id = $(this).attr('data-id');
    readURL(this, caption_id);
});

Instead of [type="file"] you could use a class selector. 可以使用类选择器代替[type="file"] For that you could: 为此,您可以:

  1. add an extra class to all inputs like class="image-box-{{id}} your-class" and then use it like $('.your-class').change(function(e){...}) 在所有输入中添加一个额外的类,例如class="image-box-{{id}} your-class" ,然后像$('.your-class').change(function(e){...})

or 要么

  1. maybe just shorten what you have to class="image-box" , because there is no reason to attach individual class to every single element, and use it like $('.image-box').change(function(e){...}) . 也许只是缩短了class="image-box" ,因为没有理由将单个类附加到每个元素上,并像$('.image-box').change(function(e){...})

And I would remove id="image-value" repeated for every element to avoid potential errors in the future or change to id="image-value-{{id}}" . 并且我将删除对每个元素重复的id="image-value"以避免将来出现潜在错误,或更改为id="image-value-{{id}}"

This event is only registered on the first element that have this id. 此事件仅在具有此ID的第一个元素上注册。

$("#image-value").change(function(e){
    var caption_id = $(this).attr('data-id');
    // alert(caption_id);
    readURL(this, caption_id);
});

That's why it only fires in the first picture. 这就是为什么它仅在第一张图片中触发。 You can change it to .image-value to make it as a class and then in your html add the image-value as class like: 您可以将其更改为.image-value以使其成为类,然后在html中将image-value添加为类,例如:

{{#images}}

<div class="picture-caption">
    <img id="{{id}}" class="caption" src="./data/picture_caption/{{picture_caption}}" alt="your image" />
    <input data-id="{{id}}" class="image-value image-box-{{id}}" type="file" style="display: none;" value="
{{picture_caption}}" />{{id}}
</div>

{{/answers}}

script: 脚本:

$(".image-value").change(function(e){
    var caption_id = $(this).attr('data-id');
    // alert(caption_id);
    readURL(this, caption_id);
});

I made the freedom to remove the id attribute because it will cause multiple same ids. 我可以自由删除id属性,因为它将导致多个相同的id。

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

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