gpt4 book ai didi

Jquery 对象数组

转载 作者:行者123 更新时间:2023-11-30 23:50:58 26 4
gpt4 key购买 nike

我在 session 中有一个对象数组。这已填充在选择列表中。根据从列表中选择的项目,我必须使用所选对象的属性预先填充表单。

请帮忙。

  • 阿努

最佳答案

您可以将员工信息存储到 JavaScript 对象中:

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

接下来绑定(bind)到选择框的更改事件:

$(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);
}
});
});

关于Jquery 对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2007932/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com