使用JSP循环创建按钮,每个按钮赋予不同的值。

huangapple go评论73阅读模式
英文:

Creating buttons with a loop in JSP, with each button giving a different value

问题

以下是翻译好的部分:

这里使用循环生成了一个数字表格。表格中的每个值都是一个按钮,当点击按钮时,弹出窗口中会显示该值的10倍。在实际项目中,数据位于JSP中,因此无法使用JavaScript。

以下是我的JSP代码:

<HTML>
<title> Hi </title>
<%
out.println("         <table border = \"1\" align=\"center\" width='600'>\n" +
                "         <tr>\n" +
                "            <th> Number </th>\n" +
                "         </tr>");
int i;
for(i=0;i<10;i++){
 out.println("         <tr>\n" +
                             "            <td style = \"text-align:center\" height=\"40\"><font style=\"color: black \">" +
                             "<button onclick=\"document.getElementById('id01').style.display='block'\" style=\"width:auto; -webkit-box-align: center; \"> " +
                             i + " </button>  </a>" +
                             "</font> </td>\n" +
                    "         </tr>   ");
}
%>
</HTML>

DIV部分:

   <div id="id01" class="modal">

        <div class="modal-content animate" >
            <div class="imgcontainer">
                <span onclick="document.getElementById('id01').style.display='none'" class="close" title="Close Modal">&times;</span>
                <img src="doctor.jpg" alt="Avatar" class="avatar">
            </div>

            <div class="container"><centre>
                <%
                    //                    
                    // 我需要显示 id*10;
                    // 
                    //
                %>
            </centre></div>


        </div>
    </div>

问题在于循环结束时i等于9,因此个别按钮的'i'值丢失。请帮忙。

英文:

Here, a table of numbers is generated with a loop. Each value in the table is a button, which, when clicked, displays 10 times the value in the pop-up. In the actual project, the data is in jsp, so I cannot use javascript.

Please help.

Here is my JSP:

&lt;HTML&gt;
&lt;title&gt; Hi &lt;/title&gt;
&lt;%
out.println(&quot;         &lt;table border = \&quot;1\&quot; align=\&quot;center\&quot; width=&#39;600&#39;&gt;\n&quot; +
                &quot;         &lt;tr&gt;\n&quot; +
                &quot;            &lt;th&gt; Number &lt;/th&gt;\n&quot; +
                &quot;         &lt;/tr&gt;&quot;);
int i;
for(i=0;i&lt;10;i++){
 out.println(&quot;         &lt;tr&gt;\n&quot; +
                             &quot;            &lt;td style = \&quot;text-align:center\&quot; height=\&quot;40\&quot;&gt; &lt;font style=\&quot;color: black &gt;&quot; + &quot;&lt;button onclick=\&quot;document.getElementById(&#39;id01&#39;).style.display=&#39;block&#39;\&quot; style=\&quot;width:auto; -webkit-box-align: center; \&quot;&gt; &quot; +i+ &quot; &lt;/button&gt;  &lt;/a&gt;&quot; +&quot;&lt;/font&gt; &lt;/td&gt;\n&quot; +
                    &quot;         &lt;/tr&gt;   &quot;);

}

%&gt;
&lt;/HTML&gt;

DIV:

   &lt;div id=&quot;id01&quot; class=&quot;modal&quot;&gt;

        &lt;div class=&quot;modal-content animate&quot; &gt;
            &lt;div class=&quot;imgcontainer&quot;&gt;
                &lt;span onclick=&quot;document.getElementById(&#39;id01&#39;).style.display=&#39;none&#39;&quot; class=&quot;close&quot; title=&quot;Close Modal&quot;&gt;&amp;times;&lt;/span&gt;
                &lt;img src=&quot;doctor.jpg&quot; alt=&quot;Avatar&quot; class=&quot;avatar&quot;&gt;
            &lt;/div&gt;

            &lt;div class=&quot;container&quot;&gt;&lt;centre&gt;
                &lt;%
                    //                    
                    // I need to display id*10;
                    // 
                    //
                %&gt;
            &lt;/centre&gt;&lt;/div&gt;


        &lt;/div&gt;
    &lt;/div&gt;

The problem is that i equals 9 by the end of the loop, so 'i' of individual buttons are lost.
Please help.

答案1

得分: 0

你可以将整个模态<div>代码写在另一个带有不同idfor循环内,每个按钮的onclick将显示相应的模态弹出框。

以下是你的 JSP 代码:

<HTML>
<title> Hi </title>
<%
out.println("         <table border = \"1\" align=\"center\" width='600'>\n" +
            "         <tr>\n" +
            "            <th> Number </th>\n" +
            "         </tr>");
int i;
for(i=0;i<10;i++){
 out.println("         <tr>\n" +
             "            <td style = \"text-align:center\" height=\"40\"><font style=\"color: black \">" + "<button onclick=\"document.getElementById('" + "id" + i + "').style.display='block'\" style=\"width:auto; -webkit-box-align: center; \"> " +i+ " </button>  </a></font> </td>\n" +
             "         </tr>   ");
}
%>
</HTML>

模态框部分:

<%  int j=0;
    for (j=0;j<10;j++){ %>

   <div id='<%= "id"+j %>' class="modal">

        <div class="modal-content animate">
            <div class="imgcontainer">
                <span onclick="document.getElementById('<%= "id"+j %>').style.display='none'" class="close" title="Close Modal">&times;</span>
                <img src="doctor.jpg" alt="Avatar" class="avatar">
            </div>

            <div class="container"><centre>
                <%= "id"+j %>
            </centre></div>


        </div>
    </div>

<%  } %>

我在TutorialsPoint上测试过,似乎运行良好。

英文:

You could write the whole modal &lt;div&gt; code inside another for loop with changing ids and the onclick for each button will display its corresponding modal popup.

Here is your JSP:

&lt;HTML&gt;
&lt;title&gt; Hi &lt;/title&gt;
&lt;%
out.println(&quot;         &lt;table border = \&quot;1\&quot; align=\&quot;center\&quot; width=&#39;600&#39;&gt;\n&quot; +
                &quot;         &lt;tr&gt;\n&quot; +
                &quot;            &lt;th&gt; Number &lt;/th&gt;\n&quot; +
                &quot;         &lt;/tr&gt;&quot;);
int i;
for(i=0;i&lt;10;i++){
 out.println(&quot;         &lt;tr&gt;\n&quot; +
                             &quot;            &lt;td style = \&quot;text-align:center\&quot; height=\&quot;40\&quot;&gt; &lt;font style=\&quot;color: black &gt;&quot; + &quot;&lt;button onclick=\&quot;document.getElementById(&#39;&quot; + &quot;id&quot; + i + &quot;&#39;).style.display=&#39;block&#39;\&quot; style=\&quot;width:auto; -webkit-box-align: center; \&quot;&gt; &quot; +i+ &quot; &lt;/button&gt;  &lt;/a&gt;&quot; +&quot;&lt;/font&gt; &lt;/td&gt;\n&quot; +
                    &quot;         &lt;/tr&gt;   &quot;);

}

%&gt;
&lt;/HTML&gt;

DIV:

&lt;%  int j=0;
    for (j=0;j&lt;10;j++){ %&gt;

   &lt;div id=&#39;&lt;%= &quot;id&quot;+j %&gt;&#39; class=&quot;modal&quot;&gt;

        &lt;div class=&quot;modal-content animate&quot; &gt;
            &lt;div class=&quot;imgcontainer&quot;&gt;
                &lt;span onclick=&quot;document.getElementById(&#39;&lt;%= &quot;id&quot;+j %&gt;&#39;).style.display=&#39;none&#39;&quot; class=&quot;close&quot; title=&quot;Close Modal&quot;&gt;&amp;times;&lt;/span&gt;
                &lt;img src=&quot;doctor.jpg&quot; alt=&quot;Avatar&quot; class=&quot;avatar&quot;&gt;
            &lt;/div&gt;

            &lt;div class=&quot;container&quot;&gt;&lt;centre&gt;
                &lt;%= &quot;id&quot;+j %&gt;
            &lt;/centre&gt;&lt;/div&gt;


        &lt;/div&gt;
    &lt;/div&gt;

&lt;%  } %&gt;

I tested it on TutorialsPoint and seems to work fine.

huangapple
  • 本文由 发表于 2020年4月6日 17:30:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/61056715.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定