首页 > 解决方案 > 当用户再次选择时,如何清理预览图像和文件?以及如何修改这个数组循环?

问题描述

在我的代码中,当用户选择图像时它可以是预览图像,但是当用户选择其他图像时如何清理它?

$(function() {
    // Multiple images preview in browser
    var imagesPreview = function(input, placeToInsertImagePreview) {
        if (input.files) {
            var filesAmount = input.files.length;
            for (i = 0; i < 3; i++) {
                var reader = new FileReader();
                reader.onload = function(event) {
                    $($.parseHTML('<img>')).attr('src', event.target.result).appendTo(placeToInsertImagePreview);
                }
                reader.readAsDataURL(input.files[i]);
            }
        }
    };

    $('#gallery-photo-add').on('change', function() {
      $(".gallery").hide();
        imagesPreview(this, 'div.gallery_1');        
    });
    
});

<div>
<div class="gallery"><img width="182" height="182" src="/themes/image_default.png"></div> 
<input type="file" id="gallery-photo-add" name="files[]" multiple  accept="image/*" required>
<div class="gallery_1"></div>
</div>

第二个问题;

如果我得到了数组值,如何简化这个?

{foreach from=$CR key=k item=v}
<select id="CustomerTitle" name="CustomerTitle" style="margin:5px;">
    {if $v.CustomerTitle == "MR."}
        <option value="MR." selected>MR.</option>
    {else}
      <option value="MR.">MR.</option>
    {/if}
    {if $v.CustomerTitle == "Mrs."}
        <option value="Mrs." selected>Mrs.</option>
    {else}
      <option value="Mrs.">Mrs.</option>
    {/if}
    </select>
{/foreach}

而这个,如果数组值为“Family,Friend,Colleague”

如何设置检查?

<input type="checkbox" id="CustomerRelation[]" name="CustomerRelation[]" value="Family" style="margin:5px;">Family
    <input type="checkbox" id="CustomerRelation[]" name="CustomerRelation[]" value="Friend" style="margin:5px;">Friend
    <input type="checkbox" id="CustomerRelation[]" name="CustomerRelation[]" value="Colleague" style="margin:5px;">Colleague

标签: phphtmlajax

解决方案


推荐阅读