我已经创建了一个jQuery UI Accordion图像加载器,它可以动态地添加或删除面板。在每个面板中都有一个图像输入表单控件,在文档的末尾是一个函数,它应该将面板中的img src更改为新选择的图像。不幸的是,我得到了:cannot read 'files' of undefined。我知道为什么要这样做,我只需要一种方式来动态添加面板,并能够更新IMGSRC的面板加载图像。
到目前为止,我的代码是:

<!DOCTYPE html>
<html lang="en">
<head>
    <!-- css links -->
    <link href="/Content/bootstrap-theme.css" rel="stylesheet" />
    <link href="/Content/bootstrap.css" rel="stylesheet" />
    <link href="/Scripts/jquery-ui-1.12.0/jquery-ui.css" rel="stylesheet" />
    <!-- /css links -->
    <!-- js files  -->
    <script src="/Scripts/jquery-3.1.0.js"></script>
    <script src="/Scripts/jquery-ui-1.12.0/jquery-ui.js"></script>
    <!-- /js files -->
</head>
<body id="myPage" data-spy="scroll" data-target=".navbar" data-offset="60">
    <script lang="en" type="text/javascript">
        $(function () {
            $("#PrimaryImageAccordion").accordion({
                collapsible: true
            });
        });
    </script>
    <br /><br />
    <button type='button' onclick='btnAddPrimaryImage_Click();' class='btn btn-default'> Add </button>
    <div id="PrimaryImageAccordion">
        <h4 class="PrimaryImageTitle">Primary Image</h4>
        <div>
            <div class="row form-group">
                <label for="ImageSelector" class="control-label col-md-2">Project Image</label>
                <div class="col-md-10">
                    <input type="file" id="ImageSelector" onchange="ImageSelector_Change(this);" /><br />
                    <img id="Image" src="#" style="width: 100px; visibility: hidden;" />
                </div>
            </div>
        </div>
    </div>
    <script lang="en" type="text/javascript">
        function btnAddPrimaryImage_Click() {
            var template = "<h4 class='PrimaryImageTitle'>Primary Image<a onclick='removePanel(this);' style='float:right'>X</a></h4>\n";
            template += "<div class='AccordionPanel'><div class='row form-group'>\n";
            template += "<label for='ImageSelector' class='control-label col-md-2'>Project Image</label>\n";
            template += "<div class='col-md-10'>\n";
            template += "<input type='file' id='Image1Selector' /><br />\n";
            template += "<img id='Image' src='#' style='width: 100px; visibility: hidden;' />\n";
            template += "</div></div></div>\n";

            $("#PrimaryImageAccordion").append(template);

            $("#PrimaryImageAccordion").accordion("refresh");
        }

        function removePanel(a) {
            $(a).parent().next().remove();
            $(a).parent().remove();
            $("#PrimaryImageAccordion").accordion("refresh");
            return false;
        }

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

                reader.onload = function (e) {
                    object.attr("src", e.target.result);
                    object.css("visibility", "visible");
                }

                reader.readAsDataURL(input.files[0]);
            }
        }
    </script>
</body>

</html>

更新1
更改了主选择器/加载程序功能,如下所示:
function ImageSelector_Change(input) {
    var object = $(input).parent().find('img#Image');

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

        reader.onload = function (e) {
            object.attr("src", e.target.result);
            object.css("visibility", "visible");
        }

        reader.readAsDataURL(input.files[0]);
    }
}

唯一的问题是它只加载第一个图像,而不加载其他图像

最佳答案

ID必须是唯一的。
因此,在函数btnAddPrimaryImage_Click中,我更正了如何使用增量变量将新元素添加到accordion中。
ImageSelector_Change函数需要获取两个参数:
这:为了获得图像
映像id将加载的映像放在何处。
此外,由于您正在使用引导程序,我建议您避免使用jQuery 3.x来解决兼容性问题。
片段:

$(function () {
  $("#PrimaryImageAccordion").accordion({
    collapsible: true
  });
});

 var idCounter = 0;
    function btnAddPrimaryImage_Click() {
        idCounter++;
        var template = "<h4 class='PrimaryImageTitle'>Primary Image<a onclick='removePanel(this);' style='float:right'>X</a></h4>\n";
        template += "<div class='AccordionPanel'><div class='row form-group'>\n";
        template += "<label for='ImageSelector'" + idCounter + " class='control-label col-md-2'>Project Image</label>\n";
        template += "<div class='col-md-10'>\n";
        template += "<input type='file' id='ImageSelector" + idCounter + "' onchange='ImageSelector_Change(this,\"Image" + idCounter + "\");' /><br />\n";
        template += "<img id='Image" + idCounter + "' src='#' style='width: 100px; visibility: hidden;' />\n";
        template += "</div></div></div>\n";

        $("#PrimaryImageAccordion").append(template);

        $("#PrimaryImageAccordion").accordion("refresh");
    }

    function removePanel(a) {
        $(a).parent().next().remove();
        $(a).parent().remove();
        $("#PrimaryImageAccordion").accordion("refresh");
        return false;
    }

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

            reader.onload = function (e) {
                document.getElementById(input).src = e.target.result;
                document.getElementById(input).style.visibility = "visible";
            }

            reader.readAsDataURL(object.files[0]);
        }
    }

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link href="https://code.jquery.com/ui/1.12.0/themes/smoothness/jquery-ui.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script>

<br /><br />
<button type='button' onclick='btnAddPrimaryImage_Click();' class='btn btn-default'> Add </button>
<div id="PrimaryImageAccordion">
    <h4 class="PrimaryImageTitle">Primary Image</h4>
    <div>
        <div class="row form-group">
            <label for="ImageSelector" class="control-label col-md-2">Project Image</label>
            <div class="col-md-10">
                <input type="file" id="ImageSelector" onchange="ImageSelector_Change(this, 'Image');" /><br />
                <img id="Image" src="#" style="width: 100px; visibility: hidden;" />
            </div>
        </div>
    </div>
</div>

关于javascript - 将动态创建的对象传递给函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39306717/

10-11 23:34
查看更多