2011-12-16 5 views
-2

私は2つのドロップダウンロケーションと部門を持つJSPを持っています。サーブレットからJSPドロップダウン値にアクセスできない

部門のドロップダウンの値は、Ajaxによるロケーションの値に基づいて設定されます。

しかし、ページを保存しようとすると、部門の最初の項目をドロップダウンした場合、部門のドロップダウンの値にアクセスできません。

しかし、2番目または3番目の項目が選択されている場合は、サーブレットからアクセスできます。

<form id="form1" name="crtdocfrm" action="CreateLocation" method="post"> 
    <fieldset width="50%"> 
    <legend>Division</legend> 
    <table id="table1"> 
     <tr class="tr_stylebutton"> 
     <td>Location Name</td> 
     <td><select name="locName" onChange="showDept(this.value)"> 
      <option value="-1">--select--</option> 
      <% 
      Iterator itrLocation = arlLocation.iterator(); 
      while (itrLocation.hasNext()) { 
      %> 
      <option value=<%=itrLocation.next()%>><%=itrLocation.next()%> 
      </option> 
      <%}%> 
     </select></td> 
     </tr> 

     <tr class="tr_stylebutton"> 
     <td>Department Name</td> 
     <td> 
      <select name="ddDeptName" onChange="alert(this.value)"></select> 
     </td> 

     </tr> 
     <tr class="tr_stylebutton"> 
     <td>Division ID</td> 
     <% 
      LocationPolulate lp = new LocationPolulate(); 
      int Div_Id = lp.DivisionId(); 
     %> 
     <td><input type="text" name="divID" disabled="true" value=<%=Div_Id + 1%>/></td> 
     </tr> 

     <tr class="tr_stylebutton"> 
     <td>Division Name</td> 
     <td><input type="text" name="divName" value=""/></td> 
     </tr> 
    </table> 

    <table border="0" width="55%" align="left" id="table-button"> 
     <tr class="tr_stylebutton"> 
     <td align="right"><input type="submit" value="Save" class="button"/></td> 
     <td align="left"><input type="submit" value="Cancel" class="button"/></td> 
     </tr> 
    </table> 
    </fieldset> 
    &nbsp;&nbsp;<br/> 
</form> 

サーブレット

protected void doPost(HttpServletRequest request, HttpServletResponse response) 
     throws ServletException, IOException { 
    processRequest(request, response); 

    int intLocId = Integer.parseInt(request.getParameter("locName")); 
    String strDivNm = (String) request.getParameter("divName"); 
    int intDeptId = Integer.parseInt(request.getParameter("ddDeptName")); 

    LocationPolulate lp = new LocationPolulate(); 
    int Res = lp.InsertDivision(strDivNm, intLocId, intDeptId); 

    if (Res == 0) { 
     request.setAttribute("Errmsg", "Error Cannot Save Division"); 
     RequestDispatcher dis1 = getServletContext().getRequestDispatcher("/AddDivision"); 
     dis1.forward(request, response); 
    } else { 
     int Div = lp.DivisionId(); 
     request.setAttribute("succmsg", "Successfully saved Division with division id:" + Div); 
     RequestDispatcher dis1 = getServletContext().getRequestDispatcher("AddDivision.jsp"); 
     dis1.forward(request, response); 
    } 
} 

任意のアイデア?

+0

のstateChanged関数(){ は、IF(xmlHttp.readyState == 4 || xmlHttp.readyState == "完全") {VAR showdata = xmlHttp.responseText。 var strar = showdata.split( ":"); \t if(strar.length == 1) \t {document.getElementById( "locName")。focus(); \tアラート( "Please Select Location Id"); \t \t \t else if(strar.length> 1) \t {option = new Option( "--- Select ---"、-1); document.crtdocfrm.ddDeptName.options [0] = option; (strar [i + 1]、strar [i]); for(var i = 0; i Alivia

+4

だけで不思議、あなたはあなたのコードが読める見つけるのですか? – BalusC

+1

optionタグIteratorで、 '.next()'メソッドを各オプションに対して2回、valueとdisplayの値のために1回ずつ呼び出すことに気づいていますか? –

答えて

0

コメントの一部としてコードを追加して申し訳ありません...私はコメントを追加しなければならなかったので、私はコード全体に適合できませんでした。Javaスクリプトの間違いがありました。正しいものが下に与えられます。あなたの提案のために...

function stateChanged() 
{ 
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") 
    { 
     var showdata = xmlHttp.responseText; 
     var strar = showdata.split(":"); 

     if(strar.length==1) 
     { 
      document.getElementById("locName").focus(); 
      alert("Please Select location Id"); 
     } 
     else if(strar.length>1) 
     { 
      option=new Option("---Select---", -1); 

      document.crtdocfrm.ddDeptName.options[0]=option; 
      for(var i=1;i<strar.length-1;i=i+2) 
      { 
       option=new Option(strar[i+1], strar[i]); 
       document.crtdocfrm.ddDeptName.options[(i-1)/2+1]=option; 
      } 
     } 
    } 
} 

は、私は別のJSPから同じ文字列のidと名前を取得し、異なる部門IDと名前の値を取得し、それをsplitingています。

関連する問題