[英]Dynamic values of javascript accessed from other HTML
我有一个带有名称为param的文本框的JSP文件。当我单击JSP表单中的按钮时,会调用javascript函数。 JS函数使用“ document.forms [0] .param.value”捕获这些值。 当我尝试通过使用警报功能在JSP中显示相同的值时,将显示该值。 但是当我使用HTML并尝试访问value.i时无法检索它,意味着它为空。但是当我使用静态值时。 取回该值。 我将随附JSP,JS和HTML文件代码。请帮助我。我在此问题上投入了相当长的时间。
这是JSP代码
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org /TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript" language="JavaScript" src="sample.js" > </script>
</head>
<body>
<form action="formproc1" name="form1" method = "GET" >
<input type=text name = "param" value=""></input>
<center> <input type="submit" value="submit"></input> </center>
<input type="button" value="click" onclick="justshow()"></input>
<input type="button" value="click1" onclick="displaystr()"></input>
</form>
</body>
</html>
这是javascript文件sample.js
function justshow()
{
str=document.forms[0].param.value;
//document.print("hii");
str1= str;
alert(str);
}
function displaystr()
{
str2=str1;
alert(str);
}
function display()
{
return "answer";
return str;
}
Here is the HTML code which accesses the sample.js file
<html>
<head>
<script type="text/javascript">
function product(a,b)
{
return a*b;
}
</script>
<script type="text/javascript" src ="C:\Documents and Settings\256160\workspace\simpleapp\WebContent\sample.js"> </script>
</head>
<body>
<script type="text/javascript">
document.write(product(1,5));
document.write(display());//this does nt
</script>
<p>The script in the body section calls a function </p>
</body>
</html>
最后一个问题是显示功能必须将JSP文本框中输入的字符串返回到HTML。
我并没有真正理解您要使用代码执行的操作,函数display()并没有执行任何操作,也没有给str分配任何内容。 当您使用js文件时,它不会将变量保存到您的计算机中,每次刷新都会导致该变量重置。 如果您要执行的操作是显示第一页到第二页的答案,则这是错误的方法,JSP的意思是将其附加到Java代码中,该Java代码应该处理表单并将某些内容输出到响应中页。 如果您想要纯HTML形式,则可以执行以下操作:
<form action="formproc1" name="form1" method="GET">
<input type="text" name="param"></input>
<input type="submit" value="submit"></input>
</form>
并在HTML响应页面中通过以下方式获取参数:
<script>
document.write(location.search.split("=")[1]);
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.