【问题标题】:how to set a combobox selected value using knockout.js?如何使用 knockout.js 设置组合框选定值?
【发布时间】:2013-06-11 19:14:54
【问题描述】:
         $.getJSON(
                    'Home/GetAllEmployees',
                    function (data) {
                        $.each(data, function (key, val) {
                            viewModel.EmployeeList.push(new EmployeeObj(val));
                        });
                    }
           );

此代码将我的 EmployeeList 加载到组合框中,没有任何问题。我有另一个有员工记录的表。每条记录都有一个分配给它的员工 ID。运行我的应用程序时,它会迭代员工记录表,如果它有员工 ID,它将将该员工 ID 发送到组合框,组合框将显示指定员工的姓名。
我的问题: 因为我是 Knockout.js 的新手,所以我对这个组合框有疑问。我不知道如何根据提供的员工 ID 显示员工姓名。

这是我将员工列表加载到 ComboBox 中的方式:

    <select data-bind="options: EmployeeList, optionsText: 'SELECTED_NAME', 
    optionsValue: 'SELECTED_ID', value: SELECTED_ID, selectedIndex: SELECTED_ID, 
    optionsCaption: 'Select Employee....' ">
    </select> 

这很好用。我可以在组合框中看到所有员工...

【问题讨论】:

  • 我看到了创建EmployeeList 的代码,但我没有看到任何组合框代码。可以发一下吗?
  • "options" binding 上的文档中的示例 4 怎么样
  • 你找到答案了吗?如果不是,我认为你应该解决你面临的问题。

标签: jquery asp.net-mvc knockout.js


【解决方案1】:

你可以这样做:

<select id="selectEmployee" data-bind="options: EmployeeList,
    optionsText: function (item) {
        return item.empName;
    },
    optionsValue: function (item) {
        return item.empId;
    },
    value: $root.selectedEmpId">
</select>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    相关资源
    最近更新 更多