如何在JSP中通过点击输入按钮将Java变量值显示在HTML输入文本框中

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

How to display java variable value in HTML input Textbox by click input button in JSP

问题

  1. **以下是我的JSP代码**
  2. <%@page import="PresentationPkg.TestCls"%>
  3. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  4. <!DOCTYPE html>
  5. <html>
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  8. <title>JSP页面</title>
  9. </head>
  10. <body>
  11. <form action="ex.jsp">
  12. <input type="text" id="resultCode" name="resultCode">
  13. <input type="button" name="viewValue" value="提交查看"/>
  14. <%
  15. String newTestResultCode = TestCls.getMaxTestResultID();
  16. %>
  17. </form>
  18. </body>
  19. </html>
  20. 我想要在点击“提交查看”按钮后,在“resultCode”文本框中显示变量“newTestResultCode”的值
英文:

Here is my JSP Code

  1. <%@page import="PresentationPkg.TestCls"%>
  2. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  3. <!DOCTYPE html>
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>JSP Page</title>
  8. </head>
  9. <body>
  10. <form action="ex.jsp">
  11. <input type="text" id="resultCode" name="resultCode">
  12. <input type="button" name="viewValue" value="Submit View"/>
  13. <%
  14. String newTestResultCode = TestCls.getMaxTestResultID();
  15. %>
  16. </form>
  17. </body>
  18. </html>

I want to show variable 'newTestResultCode' value in 'resultCode' textBox by clicking button 'Submit View'

答案1

得分: 2

  1. 尝试以下代码。不要忘记添加jQuery库:
  2. <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
  3. 复制并粘贴以下代码,然后运行:
  4. <%@page import="PresentationPkg.TestCls"%>
  5. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  6. <!DOCTYPE html>
  7. <html>
  8. <head>
  9. <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
  10. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  11. <title>JSP页面</title>
  12. </head>
  13. <body>
  14. <%
  15. String newTestResultCode = TestCls.getMaxTestResultID();
  16. %>
  17. <form action="ex.jsp">
  18. <input type="text" id="resultCode" name="resultCode">
  19. <input id="myButton" type="button" name="viewValue" value="提交查看" />
  20. </form>
  21. <script type="text/javascript">
  22. $(document).ready(function() {
  23. $('#myButton').click(function (){
  24. $("#resultCode").val("<%=newTestResultCode%>");
  25. });
  26. });
  27. </script>
  28. </body>
  29. </html>
  30. 您还可以在您的项目中下载jQuery库并使用它。这个JavaScript函数将会将您的变量数据设置到输入框中。
英文:

Try Below Code. Do not forget to add jquery js

>< script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></ script>

Copy paste below code and run it.

  1. &lt;%@page import=&quot;PresentationPkg.TestCls&quot;%&gt;
  2. &lt;%@page contentType=&quot;text/html&quot; pageEncoding=&quot;UTF-8&quot;%&gt;
  3. &lt;!DOCTYPE html&gt;
  4. &lt;html&gt;
  5. &lt;head&gt;
  6. &lt;script src=&quot;//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js&quot;&gt;&lt;/script&gt;
  7. &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;
  8. &lt;title&gt;JSP Page&lt;/title&gt;
  9. &lt;/head&gt;
  10. &lt;body&gt;
  11. &lt;%
  12. String newTestResultCode = TestCls.getMaxTestResultID();
  13. %&gt;
  14. &lt;form action=&quot;ex.jsp&quot;&gt;
  15. &lt;input type=&quot;text&quot; id=&quot;resultCode&quot; name=&quot;resultCode&quot;&gt;
  16. &lt;input id=&quot;myButton&quot; type=&quot;button&quot; name=&quot;viewValue&quot; value=&quot;Submit View&quot; /&gt;
  17. &lt;/form&gt;
  18. &lt;script type=&quot;text/javascript&quot;&gt;
  19. $(document).ready(function() {
  20. $(&#39;#myButton&#39;).click(function (){
  21. $(&quot;#resultCode&quot;).val(&quot;&lt;%=newTestResultCode%&gt;&quot;);
  22. });
  23. });
  24. &lt;/script&gt;
  25. &lt;/body&gt;
  26. &lt;/html&gt;

You can also download jquery js in your project and use it. The js function will set your variable data into input box.

答案2

得分: 1

这应该可以运行:

  1. <input type="text" id="resultCode" name="resultCode" value=<%=newTestResultCode%>>
英文:

This should work:

  1. &lt;input type=&quot;text&quot; id=&quot;resultCode&quot; name=&quot;resultCode&quot; value=&lt;%=newTestResultCode%&gt;&gt;

huangapple
  • 本文由 发表于 2020年10月21日 16:19:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/64459502.html
匿名

发表评论

匿名网友

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

确定