2016-09-13 9 views
-1

私はすべての従業員のリストから従業員の名前を選択する選択ボックスを持っています。私が達成したいのは、オブジェクトの従業員の他の属性を取得することです。私はそれを達成するためにjavascriptを使用しますが、私は常にリスト(インデックス0)から最初の要素を取得します。ここでは、コードは次のようになります。選択ボックスの値に基づいてフォームフィールドに値を設定する

<sf:select path="employee" onchange="change()" > 
    <sf:option value=""> 
     Choose employee 
    </sf:option> 
    <sf:options itemLabel="employeeName" items="${employeeList}" 
      itemValue="employeeId" /> 
    </sf:select> 

<input id="licenseNr"/> 

とJavaScript:

<script> 
    function change(){ 
     var comboIndex = document.getElementById("employee").value;   
     document.getElementById("licenseNr").value=("${employeeList.get(comboIndex).licenseNr}"); 
     } 
    </script> 

答えて

0

私はRequestParam注釈

を追加したコントローラでjqueryの

$(function() { 
     $("#employee") 
       .change(
         function(e) { 
          var option = $(this).val();        
          var newUrl = "someUrl?id="+option; 
          window.location=newUrl;        
         }); 
    }); 

と、URLにパラメータを追加することで問題を解決してきました

@RequestMapping(value = "/someUrl", method = RequestMethod.GET) 
public String showForm(@RequestParam(required = false, defaultValue="-1") 
Integer id,Model model){ 
if(id>0){ 
    Employee employee = service.fetchEmployeeById(id);  
       model.addAttribute("employee", employee);} 
    return "someUrl";} 

テキストフィールドに値を追加しました

<input id="licenseNr" value="${employee.licenseNr}" /> 
関連する問題