【问题标题】:How to display java variable value in HTML input Textbox by click input button in JSP如何通过单击JSP中的输入按钮在HTML输入文本框中显示java变量值
【发布时间】:2021-02-04 03:04:28
【问题描述】:

这是我的 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 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>

我想通过单击“提交视图”按钮在“resultCode”文本框中显示变量“newTestResultCode”值

【问题讨论】:

  • 只需在文本框中使用value="&lt;%=newTestResultCode %&gt;"
  • @Swati 这次是红色下划线。错误是找不到符号
  • 把那个jsp代码放在你输入变量之前你要获取值的地方。
  • 是的。那个问题。现在我纠正了这一点。现在工作正常。谢谢斯瓦蒂

标签: java html jsp servlets


【解决方案1】:

这应该可行:

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

【讨论】:

  • 这次是红色下划线。错误是找不到符号
  • 那是因为你在调用它之前没有实例化你的变量。您可以用 TestCls.getMaxTestResultID() 替换 value 属性中的变量名,这应该可以解决问题
  • 是的。那个问题。现在我纠正了这一点。现在工作正常。谢谢@admi
【解决方案2】:

试试下面的代码。不要忘记添加jquery js

复制粘贴下面的代码并运行它。

<%@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 Page</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="Submit View" />

  </form>

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

您也可以在您的项目中下载 jquery js 并使用它。 js函数会将你的变量数据设置到输入框中。

【讨论】:

猜你喜欢
  • 2014-04-06
  • 1970-01-01
  • 2021-05-14
  • 1970-01-01
  • 2016-03-24
  • 1970-01-01
  • 1970-01-01
  • 2013-11-03
  • 2019-05-31
相关资源
最近更新 更多