我在变量k中得到空值可以任何人帮助我PLZ .....它正在接受警报的输入而不是设置值为k我可以执行相同的没有按钮或点击事件
<% // some jsp code
String key="some value";
if(key!= null)
{
%>
<html>
<head>
<script type="text/javascript">
var val = prompt("Please enter R for Read or W for Write", "");
document.getElementById("hid").value=val;
</script>
</head>
<body>
<input type="hidden" id="hid" name="hid" />
</body>
</html>
<%
}
String k=request.getParameter("hid");
key=key+k;
System.out.println(key);
%>
答案 0 :(得分:0)
只需添加一个window.onload事件处理程序
<% // some jsp code
String key="some value";
if(key!= null)
{
%>
<html>
<head>
<script type="text/javascript">
var val = prompt("Please enter R for Read or W for Write", "");
window.onload = function() {document.getElementById("hid").value=val; }
</script>
</head>
<body>
<input type="hidden" id="hid" name="hid" />
</body>
</html>
<%
}
String k=request.getParameter("hid");
key=key+k;
System.out.println(key);
%>
Javascript在加载DOM之前执行,尝试将代码放在脚本之前(按照建议将脚本放在主体的基础上),或者如所述使用window.onload或jQuery $(function(){});