简体   繁体   English

jQuery只在第一次工作

[英]Jquery only works the first time

I am trying to teach myself general web development skills. 我正在尝试自学一般的Web开发技能。 I am trying to create a image upload with preview functionality using HTML5 FileAPI. 我正在尝试使用HTML5 FileAPI创建具有预览功能的图像上传。

Till now, I have created a file input which shows the preview of image when selected. 到现在为止,我已经创建了一个文件输入,该文件输入在选中时显示图像的预览。

Html mark up is below: HTML标记如下:

<div>
        <!-- Photos -->
        <fieldset>
            <legend>PropertyPhotos</legend>
            <div class="upload-box" id="upload-box-1">
                <div class="preview-box">
                    <img alt="Field for image cutting" id="preview_1" src="@Url.Content("~/Content/empty.png")" />
                </div>
                <div>
                    @Html.FileFor(model => model.File1)
                    @Html.ValidationMessageFor(model => model.File1)
                </div>
            </div>

            <div class="upload-box" id="upload-box-2">
                <div class="preview-box">
                    <img alt="Field for image cutting" id="preview_2" src="@Url.Content("~/Content/empty.png")" />
                </div>
                <div>
                    @Html.FileFor(model => model.File2)
                    @Html.ValidationMessageFor(model => model.File2)
                </div>
            </div>

            <div class="upload-box" id="upload-box-3">
                <div class="preview-box">
                    <img alt="Field for image cutting" id="preview_3" src="@Url.Content("~/Content/empty.png")" />
                </div>
                <div>
                    @Html.FileFor(model => model.File3)
                    @Html.ValidationMessageFor(model => model.File3)
                </div>
            </div>
        </fieldset>
    </div>

The Jquery to show preview and then display the next "upload-box" is as follows: 显示预览然后显示下一个“上载框”的Jquery如下:

<script type="text/javascript">
        $(document).ready(function () {
            // show first box
            $("#upload-box-1").fadeIn();

            //Get current & next step index
            var stepNum = $('div.upload-box').attr('id').replace(/[^\d]/g, '');
            var nextNum = parseInt(stepNum)+1;

            //Get the preview image tag
            var preview = $('#preview_'+stepNum);

            //Load preview on file tag change and display second upload-box
            $('#File'+stepNum).change(function (evt) {
                var f = evt.target.files[0];
                var reader = new FileReader();

                if (!f.type.match('image.*')) {
                    alert("The selected file does not appear to be an image.");
                    return;
                }

                reader.onload = function (e) { preview.attr('src', e.target.result); };
                reader.readAsDataURL(f);

                //Show next upload-box
                $("#upload-box-" + nextNum).fadeIn();
            });
        });
    </script>

However, this code only first for the first time ... ie on selecting a file - It shows a preview and then shows the next "upload-box". 但是,此代码仅是第一次使用,即在选择文件时-显示预览,然后显示下一个“上传框”。 However, when I browse using the second file it doesn't show any preview. 但是,当我使用第二个文件浏览时,它没有显示任何预览。

From what I have ready, I need to close the Jquery function so that it can be initialised again but I am not sure how to do that. 准备好之后,我需要关闭Jquery函数,以便可以再次对其进行初始化,但是我不确定该怎么做。

My other guess is that this JQuery is only working on document load and I need to associate it with file click. 我的另一个猜测是,此JQuery仅适用于文档加载,我需要将其与文件单击关联。

Any help will be grateful. 任何帮助将不胜感激。

Your code is executed once, when the DOM is ready the first time the page is loaded, because the page doesn't ever reload and therefore will only bind a change event handler to the element with id File1 . 当第一次加载DOM时,您的代码将执行一次,因为该页面永远不会重新加载,因此只会将change事件处理程序绑定到ID为File1的元素。 When you switch to using the file input with id File2 there's no event handler bound to it, so selecting a file does nothing. 当您切换使用ID为File2的文件输入时,没有绑定事件处理程序,因此选择文件不会执行任何操作。

You'll need to bind the event handler to the other file input elements as well, possibly using an attribute-starts-with selector: 您还需要将事件处理程序绑定到其他文件输入元素,可能使用attribute-starts-with选择器:

$('[id^="File"]').change(...);

or by adding a class to each of those elements and selecting based on that. 或通过向这些元素中的每个元素添加类并基于该元素进行选择。

You'll also need to handle incrementing the value of nextNum when appropriate, so that subsequent calls display the next set of elements correctly. 您还需要在适当的时候处理递增nextNum的值,以便后续调用正确显示下一组元素。

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

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