简体   繁体   English

Spring根据选择框中的值填充表单字段

[英]Spring populating form fields based on value from select box

I have select box where I select name of employee from list of all employees.我有选择框,我可以从所有员工的列表中选择员工姓名。 What I want to achieve is to get other attributes of object employee.我想要实现的是获取对象员工的其他属性。 I use javascript to achieve it but I always get the first element from the list (index 0).我使用 javascript 来实现它,但我总是从列表中获取第一个元素(索引 0)。 Here is the code:这是代码:

<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"/>

And javascript:和 javascript:

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

I have solved problem by adding parameter in url with jquery我通过在 url 中使用 jquery 添加参数解决了问题

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

In controller I have added RequestParam annotation在控制器中,我添加了 RequestParam 注释

@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";}

Then I have added value to text field然后我为文本字段添加了值

<input id="licenseNr" value="${employee.licenseNr}" />  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM