日期:2014-05-18  浏览次数:20678 次

javascript奇怪问题,在线求助
<form   name= "sform "   method= "post "   action= "addStudent.jsp ">
<input   type= "hidden "   name= "id "   value= " <%=request.getParameter( "id ")%> ">
<input   type= "submit "   name= "submit "   value= "确定 "
onclick= "setformaction() "> </td>
</form>
<script   type= "text/javascript ">
function   setformaction(){
alert(document.sform.action)               //1
var   id   =   document.sform.id.value;
alert(id);                                                   //2
if(id!=null){
document.sform.action= "updateStudent.jsp ";
}
alert(document.sform.action)               //3
}
</script>
结果是:
1、addStudent.jsp
2、null
3、updateStudent.jsp
晕倒,难道在jsp里null!=null返回true?恳请帮忙!Q!

------解决方案--------------------
if(id!= 'null '){
------解决方案--------------------

建议var id...不要用id这个变量,尽量避免重名问题,用aaa感觉都比id要好;

------解决方案--------------------
测了一下,好像没什么问题。

-----------------------------
<%@ page language= "java " import= "java.util.* " pageEncoding= "UTF-8 "%>

<%@taglib uri= "http://java.sun.com/jsp/jstl/core " prefix= "c "%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN ">
<html>
<head>
<title> My JSP 't.jsp ' starting page </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 ">
<script type= "text/javascript ">
function setformaction()
{
alert(document.sform.action) //1

var id = document.sform.id.value;
alert(id); //2

if(id!=null)
{
document.sform.action= "updateStudent.jsp ";
}

alert(document.sform.action) //3
}
</script>
</head>
<body>
<form name= "sform " method= "post " action= "addStudent.jsp ">
<input type= "hidden " name= "id " value= "${param.id} ">
<br>
<input type= "submit " name= "submit " value= "确定 "
onclick= "setformaction(); ">
</form>
</body>
</html>

-----------------------
http://localhost:8080/test/t.jsp?id=1
对话框显示:
addStudent.jsp
1
updateStudent.jsp


------解决方案--------------------
if(id!= " ")
------解决方案--------------------