我有个页面类似资料登记一样,开始一个下拉列表框,比如所有的客户,当选某一客户,就在该页面出现这客户的姓名,联系方式等信息。我是这样实现的:
 <% List compList=(List)request.getAttribute("list");%>
 <select name="comp_id" onchange="change();">
         <%             for(int i=0;i<compList.size();i++)
                     {
                      Object[]obj=(Object[])compList.get(i);
              %>        
              <option value="<%=obj[0] %>" ><%=obj[1]%>-<%=obj[2]%></option>  
              
          <%
                     
                     }
          %>         
    
      </select>
这里的list可以得到公司信息,当onchange()后,调用JS
<script language="javascript">
function change()
{
   document.forms[0].action='getfilelist.do?action=getCompsInfo;
   document.forms[0].submit();
}
</script>
现在可以通过JS调用action,问题是如何把那个select选中值comp_id传到action中?
我在action里打印comp_id为空值,请高手指教错在哪?或是如何传这值

解决方案 »

  1.   


    <% List compList=(List)request.getAttribute("list");%> 
    <select name="comp_id" id="comp_id" onchange="change();"> 
          <%            for(int i=0;i <compList.size();i++) 
                        { 
                        Object[]obj=(Object[])compList.get(i); 
                  %>        
                  <option value=" <%=obj[0] %>" > <%=obj[1]%>- <%=obj[2]%> </option>  
                  
              <% 
                        
                        } 
              %>        
        
        </select>
    <script language="javascript"> 
    function change() 

      var comp_id=document.getElementById("comp_id").value
      document.forms[0].action="getfilelist.do?action=getCompsInfo&comp_id"+comp_id; 
      document.forms[0].submit(); 

    </script> 
      

  2.   

    <script language="javascript"> 
    function change() 

      var comp_id=document.getElementsByName('comp_id')[0].value;//通过name得到值,不用加那个id属性也可以。
      document.forms[0].action='getfilelist.do?action=getCompsInfo&comp_id'+comp_id; 
      document.forms[0].submit(); 
    } 估计你没有得到值的跟本原因是在于你没有把你的select放到form里吧。
      

  3.   

    奇怪,我按3楼写的,我的IE里这样显示的:
    http://localhost:8080/express/getfilelist.do?action=getCompsInfo&compId=112
    提示500错误,
    java.lang.NumberFormatException: null
    com.exp.hecny.action.GetFileListAction.execute(GetFileListAction.java:50)这说明compId可以传过去吧,可是在action里的50,
    String comp_id=request.getParameter("compId");
     Long compId=Long.parseLong(comp_id);是这句
    这是什么原因呢?
      

  4.   

    你先打印一下这句话是否取到了值
    String comp_id=request.getParameter("compId"); 
      

  5.   

    你这种取值的方法很不好,如果页面刷新,会将填写的信息丢失。
    我看见你已经把客户的信息取到页面compList,那就可以把信息保存到javascrpt数组,然后通过数组显示客户信息,这样,会降低数据库的负载,也减少了代码编写.
    <script type="text/javascript">
     var comList = new ArrayList();
    </script>
    <% List compList=(List)request.getAttribute("list");%> 
    <select name="comp_id" id="comp_id" onchange="change();"> 
          <%            for(int i=0;i <compList.size();i++) 
                        { 
                        Object[]obj=(Object[])compList.get(i); 
                  %>
                  <script type="text/javascript">
                        compList[<%=i%>][0] =<%=obj[0]%>;
                        compList[<%=i%>][1] =<%=obj[1]%>;
                        compList[<%=i%>][2] =<%=obj[2]%>;
                        //......
                  </script>       
                  <option value=" <%=i %>" > <%=obj[1]%>- <%=obj[2]%> </option>  
                  
              <% 
                        
                        } 
              %>        
        
        </select> 
    <script language="javascript"> 
    function change() 

      var comp_id=document.getElementById("comp_id").value 
      str showMsg = "客户编号" + compList[comp_id][0] + "<br>"
                    "客户姓名" + compList[comp_id][1]+ "<br>...";
      document.getElementById("显示信息Div的id").innerHTML=showMsg;

    </script> 
      

  6.   

    应该是这个原因。PS:java.lang.NumberFormatException: null 这个异常是Long compId=Long.parseLong(comp_id);com_id是个""或者是null转换出错。