[英]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.