【问题标题】:passing the value of an array to a method in javascript将数组的值传递给javascript中的方法
【发布时间】:2014-01-16 11:36:56
【问题描述】:

我必须创建一个 HTML 表格,我将在其中显示图像。此外,我想将此路径传递给下一个 servlet 页面。为此,我在 javascript 中创建了一个单独的方法。现在的问题是,每当我单击任何图像时,它每次都会通过相同的路径。请给我这个问题的任何解决方案,或者告诉我将路径传递到下一页的任何替代方案。 我的代码是--->

<%@page import = "java.util.*" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<html>
    <head>
        <link href="Style.css" rel="stylesheet" type="text/css"/>
        <title>Home</title>
    </head>
    <body>
        <center>
            <h3>${requestScope.payment}</h3>
            <jsp:include page="Header.jsp"/>
            <jsp:include page="Menu2.jsp"></jsp:include>
            <form method="post" action="ProductFeatures" id="myform">
                <table border="1" bordercolor="green" bgcolor="yellow" align="center" id="store" >
                </table>
            </form>
            <%
             ArrayList<String> l = null;
             if(request.getAttribute("list") instanceof ArrayList<?>){
                 l = (ArrayList<String>)request.getAttribute("list");
             }
             %>
             <script type="text/javascript">
             window.onload = function(){
                 var path = new Array();
                 var imagepath = new Array();
                 var table = document.getElementById("store");
                 var j=0;
                 var k=0;
                 var row = null;
                 <%for(int i=0;i<l.size();i++) {%>
                     path.push("<%=l.get(i)%>");
                 <%}%>
                 for(i=0;i<path.length;i++){
                     imagepath[i] =path[i].replace ("F:java_projectsApplication4images","F:\\java_projects\\Application4\\images\\");
                 }
                 for(i=0;i<path.length;i++){
                     if(i%4==0){
                         row = table.insertRow(j);
                         k=0;
                         j++;
                     }
                     var data = imagepath[i];
                     var cell = row.insertCell(k);
                     var image = document.createElement("img");
                     image.setAttribute("src",imagepath[i]);
                     image.setAttribute("height","160");
                     image.setAttribute("width","120");
                     image.setAttribute("onclick",function(){getDetails(data);});
                     cell.appendChild(image);
                     row.appendChild(cell);
                     k++;
                 }
             };

             function getDetails(imagepath){
                 document.write(imagepath);
                 if(path.length>10){
                     var form = document.getElementById("myform");
                     var input = document.createElement("input");
                     input.type="hidden";
                     input.value=imagepath;
                     input.name="imagepath";
                     form.appendChild(input);
                     form.submit();
                 }
             }
            </script>
            <jsp:include page="Footer.jsp"/>
        </center>
    </body>
</html>

在此代码中,函数 getDetails 变量 imagepath 始终包含相同的值。请有人告诉我这段代码中的错误在哪里。我没有正确理解它。

【问题讨论】:

    标签: java javascript html jsp servlets


    【解决方案1】:

    更改以下行:

    image.setAttribute("onclick",function(){getDetails(data);});
    

    进入:

    image.setAttribute("onclick",(function(d){return function(){getDetails(d);}}(data));
    

    还有一行:

    row.appendChild(cell);
    

    是冗余的,因为单元格是用 row.insertCell(k) 插入的

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多