不太明白这个问题,自己随便写了个测试页面:在session中放一个值,通过javascript的check()函数去比较输入的值与session存的值是否相同,感觉上用<%=session.getAttribute("name")%>这样应该对了,但是页面就是直接跳转,郁闷啊,高手指点下
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<%
session.setAttribute("name","zhangsan");
%>
<title>test</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript">
function check(){
if(document.form1.name.value!= "<%=session.getAttribute("name")%>"){
alert("ok");
}
}
</script>
</head>
<body>
<form action="/hahha/index.jsp">
<input type="text" name="name" id="name"/>
<input type="submit" name="ok" onClick="return check()"/>
</form>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<%
session.setAttribute("name","zhangsan");
%>
<title>test</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript">
function check(){
if(document.form1.name.value!= "<%=session.getAttribute("name")%>"){
alert("ok");
}
}
</script>
</head>
<body>
<form action="/hahha/index.jsp">
<input type="text" name="name" id="name"/>
<input type="submit" name="ok" onClick="return check()"/>
</form>
</body>
</html>
。return false;
}
你得返回false,否则页面会提交的。
你这个需要把<%=session.getAttribute("name")%>作为参数来获得值。
function check(sessionValue){
if(document.form1.name.value!= sessionValue){
alert("ok");
}
}调用:
<input type="submit" name="ok" onClick="return check(<%=session.getAttribute("name")%>)"/>
你在试试
你这个问题是你的form没有name属性 你需要把<form name="form1">中的name属性写上。。
if(document.form1.name.value!= " <%=session.getAttribute("name")%>"){ 你这一行的!=后面多个空格。
你还需要再加个return false;
在函数中返回一个false;