请教java和javascript联动,解决下拉框没有被选中问题. 在java代码里面includeout.println("出你的javascript");代码 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 有没有具体一点的啊! 这个是传过来的参数<% if(request.getAttribute("list")!=null) { String sheng = request.getAttribute("list").toString(); out.print(sheng); }%>这个是下拉框<select name="select1" onchange="goThere()"> <option value="00" >请选择</option> <option value="31" >上海</option> <option value="12" >天津</option> <option value="13" >河北</option> <option value="14" >山西</option></select>我传过来一个list比如是上海,上海就被选中,如果传过来的是山西山西就被选中。 这个你最好用xmlhttp+js+DHTML做,也就是现在说的很火的AJAX做。关于具体的,你可以上网随便找一个AJAX的例子,看一下,基本的就会了。 我想用jsp做啊,ajax我不太懂啊! jsp是做不了的 至少要用到js. 我是想jsp+javascript做,但是不知怎么做。 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title>My JSP 'index.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"> </head> <body> This is my JSP page. <br> <table> <select> <option value=""></option> <option id="hello" value="hello">hello</option> <option id="else" value="else">else</option> <option id="whatever" value="whatever">else</option> </select> </table> </body> <% String s="else"; if(s.equals("hello")){ %> <script> document.getElementById("hello").selected=true; </script> <%}else if(s.equals("else")){ %> <script> document.getElementById("else").selected=true; </script> <%} else{%> <script> document.getElementById("whatever").selected=true; </script> <%} %></html>如果你不想用AJAX,上面的就可以,其中定义的变量s就代表你后台传递过来的值。把上面的内容保存一个jsp文件,然后随便在一个servlet容器中运行一下,你也可以修改一下s的值。 我需要一个一个判断吗?我有二十多个option,getElementById得到的是option的value,我想比较option的text的属性。 想要这样的效果JS就可以1.htm<body onload='test("list","listcity");'><select name="listcity" id="listcity"><option value="上海">上海</opiton><option value="北京">北京</opiton><option value="天津">天津</opiton><option value="广州">广州</opiton></select><script language=javascript>function test(param,sel) { //param为你要指定的参数名,sel为selected名称 var str = window.location.search; var ar = new Array(); str = str.substring(1); var obj = document.getElementById("listcity"); ar = str.split("&"); var i,val; for(i=0;i<ar.length;i++) { if(ar[i].indexOf(param+"=")!=-1) {val = ar[i].substring(ar[i].indexOf(param+"=")+param.length+1);break; } } for(var j=0;j<obj.options.length;j++) { if(val==obj.options[j].value) { obj.options[j].selected=true;break; } } } </script>测试:1.htm?list=北京看看效果 我试过了,你的例子是可以的,就是装载的时候调用test方法,就是我一开始是没有参数的,等选择了下拉框之后,从其他页面调来的参数。这样子好象会有一些问题。能不能做一个有一个<% String sheng = request.getAttribute("list").toString();%>之后调用test()的啊 <% String sheng = request.getAttribute("list").toString();%><script language="javascript">var opts = document.all("select1").options;for(i=0; i<opts.length; i++){ if(opts[i].value == '<%=sheng%>'){ opts[i].selected = true; }}</script> 还是有一点问题,就是这个<%=sheng%>的参数会报错误,因为一开始是不存在这个对象的,我选中下拉框之后返回一个list,然后转换成字符串才有了这个sheng的对象。所以在网页一打开之后就会报错,不知怎么解决这个问题。 java做服务端C++做客户端传文件问题...高手指点下 jboss下如何部署jsp及class文件 一个数组引用的问题? weblogic 8中console的页面都放在哪里了? 怎样装载装载XML文件? tomcat的问题? JBuider中数据库文件jsd文件怎么生成的呀? 500分求购j2ee本身的源代码 请问什么是“持久化”? jsp的问题 Unable to compile class for JSP 如何在struts中使用displaytag? 项目中遇到的问题????
这个是传过来的参数
<%
if(request.getAttribute("list")!=null)
{
String sheng = request.getAttribute("list").toString();
out.print(sheng);
}
%>
这个是下拉框
<select name="select1" onchange="goThere()">
<option value="00" >请选择</option> <option value="31" >上海</option> <option value="12" >天津</option> <option value="13" >河北</option> <option value="14" >山西</option>
</select>
我传过来一个list比如是上海,上海就被选中,如果传过来的是山西山西就被选中。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.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">
</head>
<body>
This is my JSP page. <br> <table>
<select>
<option value=""></option>
<option id="hello" value="hello">hello</option>
<option id="else" value="else">else</option>
<option id="whatever" value="whatever">else</option>
</select>
</table>
</body>
<%
String s="else";
if(s.equals("hello")){
%>
<script>
document.getElementById("hello").selected=true;
</script>
<%}else if(s.equals("else")){ %>
<script>
document.getElementById("else").selected=true;
</script>
<%} else{%>
<script>
document.getElementById("whatever").selected=true;
</script>
<%} %>
</html>
如果你不想用AJAX,上面的就可以,其中定义的变量s就代表你后台传递过来的值。把上面的内容保存一个jsp文件,然后随便在一个servlet容器中运行一下,你也可以修改一下s的值。
<body onload='test("list","listcity");'>
<select name="listcity" id="listcity">
<option value="上海">上海</opiton>
<option value="北京">北京</opiton>
<option value="天津">天津</opiton>
<option value="广州">广州</opiton>
</select>
<script language=javascript>function test(param,sel) { //param为你要指定的参数名,sel为selected名称
var str = window.location.search;
var ar = new Array();
str = str.substring(1);
var obj = document.getElementById("listcity");
ar = str.split("&");
var i,val;
for(i=0;i<ar.length;i++)
{
if(ar[i].indexOf(param+"=")!=-1)
{val = ar[i].substring(ar[i].indexOf(param+"=")+param.length+1);break; }
}
for(var j=0;j<obj.options.length;j++)
{
if(val==obj.options[j].value) {
obj.options[j].selected=true;break;
}
}
}
</script>测试:
1.htm?list=北京看看效果
<%
String sheng = request.getAttribute("list").toString();
%>
之后调用test()的啊
String sheng = request.getAttribute("list").toString();
%>
<script language="javascript">
var opts = document.all("select1").options;
for(i=0; i<opts.length; i++){
if(opts[i].value == '<%=sheng%>'){
opts[i].selected = true;
}
}
</script>