[英]Dynamic values of javascript accessed from other HTML
I hve a JSP file which has a text boxes with name param.when i click a button in the JSP form a function of a javascript is called. 我有一个带有名称为param的文本框的JSP文件。当我单击JSP表单中的按钮时,会调用javascript函数。 The values are trapped by the JS function using "document.forms[0].param.value".
JS函数使用“ document.forms [0] .param.value”捕获这些值。 When i try to display the same value in the JSP by using alert feature the value gets displayed.
当我尝试通过使用警报功能在JSP中显示相同的值时,将显示该值。 But when i use a HTML and try accessing the value.i am not able to retrieve it ,meaning it is empty.But when i use a static value.
但是当我使用HTML并尝试访问value.i时无法检索它,意味着它为空。但是当我使用静态值时。 The value is retrieved.
取回该值。 I will enclose the JSP ,JS and HTML file code.Please help me .I worked on this issue for considerable time.
我将随附JSP,JS和HTML文件代码。请帮助我。我在此问题上投入了相当长的时间。
Here is the JSP code 这是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>
Here is the javascript file sample.js 这是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>
Finally the question is THE DISPLAY FUNCTION MUST RETURN THE STRING ENTERED IN THE JSP TEXT BOX TO THE HTML . 最后一个问题是显示功能必须将JSP文本框中输入的字符串返回到HTML。
I don't really get what you're trying to do with your code, the function display() is not doing anything, it's not assigning str anything. 我并没有真正理解您要使用代码执行的操作,函数display()并没有执行任何操作,也没有给str分配任何内容。 When you use the js file it doesn't save variables to your computer, each refresh will cause the variable to reset.
当您使用js文件时,它不会将变量保存到您的计算机中,每次刷新都会导致该变量重置。 If what you are trying to do is display the answer from the first page to the second then this is the wrong way to do this, JSP's are meant to be attached to java code that is suppose to process the form and output something to the response page.
如果您要执行的操作是显示第一页到第二页的答案,则这是错误的方法,JSP的意思是将其附加到Java代码中,该Java代码应该处理表单并将某些内容输出到响应中页。 If you want a purely html form then you can do this:
如果您想要纯HTML形式,则可以执行以下操作:
<form action="formproc1" name="form1" method="GET">
<input type="text" name="param"></input>
<input type="submit" value="submit"></input>
</form>
and in the HTML response page you get the param through this: 并在HTML响应页面中通过以下方式获取参数:
<script>
document.write(location.search.split("=")[1]);
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.