在java代码里面include
out.println("出你的javascript");
代码

解决方案 »

  1.   

    有没有具体一点的啊!
     这个是传过来的参数
    <%
    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比如是上海,上海就被选中,如果传过来的是山西山西就被选中。
      

  2.   

    这个你最好用xmlhttp+js+DHTML做,也就是现在说的很火的AJAX做。关于具体的,你可以上网随便找一个AJAX的例子,看一下,基本的就会了。
      

  3.   

    我想用jsp做啊,ajax我不太懂啊!
      

  4.   

    jsp是做不了的 至少要用到js.
      

  5.   

    我是想jsp+javascript做,但是不知怎么做。
      

  6.   


    <!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的值。
      

  7.   

    我需要一个一个判断吗?我有二十多个option,getElementById得到的是option的value,我想比较option的text的属性。
      

  8.   

    想要这样的效果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=北京看看效果
      

  9.   

    我试过了,你的例子是可以的,就是装载的时候调用test方法,就是我一开始是没有参数的,等选择了下拉框之后,从其他页面调来的参数。这样子好象会有一些问题。能不能做一个有一个
    <%
        String sheng = request.getAttribute("list").toString();
    %>
    之后调用test()的啊
      

  10.   

    <%
        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>
      

  11.   

    还是有一点问题,就是这个<%=sheng%>的参数会报错误,因为一开始是不存在这个对象的,我选中下拉框之后返回一个list,然后转换成字符串才有了这个sheng的对象。所以在网页一打开之后就会报错,不知怎么解决这个问题。