英文:
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.
<%@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>
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:
<input type="text" id="resultCode" name="resultCode" value=<%=newTestResultCode%>>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论