Jquery对象数组

问题描述:

我有一个会话中的对象数组。这已填充到选择列表中。基于从列表中选择的项目,我将不得不预先使用所选对象的属性填充表单。Jquery对象数组

请帮忙。

  • Aanu
+0

请提供你正在使用哪种服务器技术;澄清“session”(传统PHP $ _SESSION等)的意思;最后,张贴你迄今为止所尝试的内容。 – 2010-01-05 17:44:03

+0

其java会话。我只是填充选择作为下。 #foreach($ emp中的$ allEmp)

你可以员工的信息存储到一个JavaScript对象:

var employees = [ 
    { id: '1', sex: 'm', city: 'Paris' }, 
    { id: '2', sex: 'f', city: 'London' }, 
    ... etc fill this in a foreach loop 
]; 

下一页绑定到选择框的更改事件:

$(function() 
{ 
    $('select#id_of_the_employees_select').change(function(e) { 
     // Get the current selected employee id 
     var selectedEmployeeId = $(this).val(); 
     // find the corresponding employee in the list 
     var emps = $.grep(employees, function(n, i) { 
      return n.id == selectedEmployeeId; 
     }); 
     if (emps.length > 0) { 
      var employee = emps[0]; 
      // fill the form elements with employee data: 
      $('#employee_sex').val(employee.sex); 
      $('#employee_city').val(employee.city); 
     } 
    }); 
}); 
+0

工作。感谢代码。 – Aanu 2010-01-05 18:53:44