日期:2014-05-16  浏览次数:20414 次

求大神指点那!!!!感激不尽那!!!JS给Input赋值问题!!!纠结一个晚上了
我就是想把mainTest中的值通过JS赋给一个hidden类型的input的value,然后在score.jsp页面通过
request.getParameter取得mainTest页面中hidden类型的input的value,可是试了好多办法都传不过去
!!!!都为空啊,可是网上都说好使为什么我用就不好使了呢!!!???崩溃呀!!!各路大神救命啊!!!
mainTest.jsp
<%@ page language="java" contentType="text/html; charset=gb2312"
    pageEncoding="gb2312"%>
<!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>
</head>
<body>


<form action="score.jsp" method="post" name="mainTest" id="mainTest" >

    <iframe src="test.jsp" width="1200" height="1000" id="quesFrame" name="ques">
    </iframe>

    <input type="hidden" value="" name="selectionOfStu" id="selectionOfStu"  />
    
    <p><input type="submit" value="我要交卷" onClick=getSlcAnswer() /></p>
</form>
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js">
        function getSlcAnswer(){
            document.mainTest.getElementById("selectionOfStu").value="ma";
            
        }
    </script>
</body>
</html>

score.jsp
<%@ page language="java" contentType="text/html; charset=gb2312"
    pageEncoding="gb2312"%>
<!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>
</head>
<%  

    String strSlcStu=request.getParameter("selectionOfStu");
    System.out.println("slc:"+strSlcStu);
    
%>
<body>

</body>
</html>

------解决方案--------------------
 function getSlcAnswer(){
            document.getElementById("selectionOfStu").value="ma";
            
        }


直接这样就行了,getElementById方法只有document有。

其他dom元素没有这个方法的。一般来说一个页面上不会出现重复id的情况,所以没有必要每个dom节点都有自己的getElementById方法。