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

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

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

问题

**以下是我的JSP代码**

    <%@page import="PresentationPkg.TestCls"%>
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>JSP页面</title>
        </head>
        <body>
            <form action="ex.jsp"> 
          <input  type="text" id="resultCode" name="resultCode">
          <input  type="button" name="viewValue" value="提交查看"/>
          <%
            String newTestResultCode = TestCls.getMaxTestResultID();
          %>
          </form>
        </body>
    </html>
我想要在点击“提交查看”按钮后,在“resultCode”文本框中显示变量“newTestResultCode”的值
英文:

Here is my JSP Code

<%@page import="PresentationPkg.TestCls"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <form action="ex.jsp"> 
      <input  type="text" id="resultCode" name="resultCode">
      <input  type="button" name="viewValue" value="Submit View"/>
      <%
        String newTestResultCode = TestCls.getMaxTestResultID();
      %>
      </form>
    </body>
</html>

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

答案1

得分: 2

尝试以下代码。不要忘记添加jQuery库:

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

复制并粘贴以下代码,然后运行:

<%@page import="PresentationPkg.TestCls"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP页面</title>
</head>
<body>
<%
    String newTestResultCode = TestCls.getMaxTestResultID();
%>
<form action="ex.jsp"> 
<input  type="text" id="resultCode" name="resultCode">
<input  id="myButton" type="button" name="viewValue" value="提交查看" />

</form>

<script type="text/javascript">
$(document).ready(function() {
    $('#myButton').click(function (){
       $("#resultCode").val("<%=newTestResultCode%>");
    });
});
</script>
</body>
</html>

您还可以在您的项目中下载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.

&lt;%@page import=&quot;PresentationPkg.TestCls&quot;%&gt;
&lt;%@page contentType=&quot;text/html&quot; pageEncoding=&quot;UTF-8&quot;%&gt;
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
 &lt;script src=&quot;//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js&quot;&gt;&lt;/script&gt;
    &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;
    &lt;title&gt;JSP Page&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
  &lt;%
    String newTestResultCode = TestCls.getMaxTestResultID();
  %&gt;
    &lt;form action=&quot;ex.jsp&quot;&gt; 
  &lt;input  type=&quot;text&quot; id=&quot;resultCode&quot; name=&quot;resultCode&quot;&gt;
  &lt;input  id=&quot;myButton&quot; type=&quot;button&quot; name=&quot;viewValue&quot; value=&quot;Submit View&quot; /&gt;

  &lt;/form&gt;

   &lt;script type=&quot;text/javascript&quot;&gt;
    $(document).ready(function() {
        $(&#39;#myButton&#39;).click(function (){
           $(&quot;#resultCode&quot;).val(&quot;&lt;%=newTestResultCode%&gt;&quot;);
        });
    });
   &lt;/script&gt;
&lt;/body&gt;
&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

这应该可以运行:

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

This should work:

&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:

确定