如何在jsp页面中获取js的变量值
今天做一个jsp页面的多个下拉列表联动的问题,发现无法在java中直接获取js的变量的值,麻烦高手帮忙解决下,谢谢。我的打算是获取js的变量值,之后用java获取字符串,并与js的变量值拼接,得到js中的变量名。下面是一个三个表的联动,因为这个页面后面还有一个5个表联动的,所以打算把那个联动的function作成一个公共模块,可是现在调试不过去,总是说对象不存在。例如,我用java输出之后的结果我想得到subcatstudyYear,可是现在的到的是subcatoper,oper为function获取得参数名,我想获得oper的值studyYear。
源代码如下:
<script lanuage=javascript>
<%
out.println( "var onecountstudyYear; ");//学年
out.println( "var onecountstudyTerm; ");//学期
out.println( "subcatstudyYear = new Array(); ");//学年
out.println( "subcatstudyTerm = new Array(); ");//学期
//获取所有学年信息
String[] strParentStudyYear = dbop.select( "select distinct g.Grade_Name,g.Grade_ID,cb.StudyLength from tbGrade g,tbTerm_Manage t,tbCourse_Begin cb where t.Academic_Year=g.Grade_ID and t.termID=cb.TermID ").split( "; ");
//获取的数据不同记录之间用“;”分割,同一条记录不同字段之间用“,”分割
for(int i=0; i <strParentStudyYear.length; i++){
String[] strChildStudyYear = strParentStudyYear[i].split( ", ");
out.println( "subcatstudyYear[ "+i+ "] = new Array(\ " "+strChildStudyYear[0]+ "\ ",\ " "+strChildStudyYear[2]+ "\ ",\ " "+strChildStudyYear[1]+ "\ "); ");
}
//输出学年总数
out.println( "onecountstudyYear= "+strParentStudyYear.length);
//获取所有学期信息
String[] strParentStudyTerm = dbop.select( "select Term,termID,Academic_Year from tbTerm_Manage ").split( "; ");
for(int i=0; i <strParentStudyTerm.length; i++){
String[] strChildStudyTerm = strParentStudyTerm[i].split( ", ");
out.println( "subcatstudyTerm[ "+i+ "] = new Array(\ " "+strChildStudyTerm[0]+ "\ ",\ " "+strChildStudyTerm[2]+ "\ ",\ " "+strChildStudyTerm[1]+ "\ "); ");
}
//输出学期总数
out.println( "onecountstudyTerm= "+strParentStudyTerm.length);
%>
//级联查询
function changelocation(oper,locationid){
document.myform.Nclassid.length = 0;
var locationid=locationid;
var oper=oper;
var i;
<%out.print( "for (i=0;i < onecount ");%> oper <%out.print( "; i++)\n ");%>
<%out.print( "if (subcat ");%> oper <%out.print( "[i][1] == locationid)\n ");%>
<% out.print( "document.all.toggleDemoTable.all.topbar.all. "+);%> oper <% out.print(+ ".options[document.all.toggleDemoTable.all.topbar.all. ");%> oper <% out.print( ".length] = new Option(subcat ");%> oper <% out.print( "[i][0], subcat ");%> oper <% out.print( "[i][2]);\n "); %>
}
</script>
</head>
<body topmargin= "10 " leftmargin= "10 " ondrag= "return false " >
<table class= "toolBar " id= "toggleDemoTable " class= "tex004 ">
<tr> <td> 学制 <select name= "studyLength " id= "studyLength " size= "1 " onChange= "changelocation( 'studyYear ',this.value) "> <option selected value= "1 "> 三年 </option> <option selected value= "1 "> 四年 </option> </select>
</td>
<td id= "topbar "> 学年 <select name= "studyYear " id= "studyYear " size= "1 " onChange= "changelocation( 'studyTerm ',this.value) ">
<option selected value= " "> ==请选择学年== </option>
</select>
</td>
<td> 学期 <select id= "studyTerm " name= "studyTerm ">
<option selected value= " "> ==请选择学期== </option>
</select>
</td>
</tr>
</table>
</body>
</html>
[解决办法]
直接传值啊,
<script>
var a= "123 ";
document.all( "text1 ").value=a;
</script>
<tr> <td> <input type= "text " name= "text1 "> </td> </tr>
[解决办法]
js只能操作页面上的元素,而java代码是在后台就执行玩了,所以想js操作java的变量,可以在页面放置一个隐藏域,将java变量值放在隐藏域的value中。这样js就可以操作这个变量了。
java操作js,也是通过隐藏域互通。
Ajax实现三级联动下拉框
http://www.blogjava.net/rickhunter/articles/62571.html