从knockout.js 中的下拉列表更新对象 [英] Updating objects from a drop down list in knockout.js

查看:23
本文介绍了从knockout.js 中的下拉列表更新对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试编辑下拉列表中的对象.选择项目时,namesuffix 显示在可编辑的文本字段中;我可以编辑(和保存)name,但对suffix 的更改不会坚持.

Trying to edit objects in a drop-down list. When selecting an item, name and suffix display in editable text fields; I can edit (and save) name, but changes to suffix don't stick.

这是 JS 代码,其余的在 fiddle 中:http://jsfiddle.net/raffian/4kXXc/1/

Here's the JS code, the rest in on fiddle: http://jsfiddle.net/raffian/4kXXc/1/

function Domain(n){
    var self = this;
    self.name = ko.observable(n);
    self.suffix = ko.observable();
};

function DomainsViewModel(){
    var self = this;
    self.domains = ko.observableArray([]);
    self.newDomain = ko.observable("");
    self.selectedDomain = ko.observable();

    self.addNewDomain = function() {
        self.domains.push(new Domain(self.newDomain()));
        self.newDomain('')
    };
};

ko.applyBindings(new DomainsViewModel());

推荐答案

不知道它不起作用的技术原因(有人知道吗?)

Don't know the technical reason why it doesn't work (anybody knows?)

一个可行的解决方案是删除 selectedDomain(). 并通过在 tr 或 td 元素中放置一个 with 绑定来替换它 data-bind="with: selectedDomain"

A working solution is to remove selectedDomain(). and replace it by placing a with binding in either tr or td elements data-bind="with: selectedDomain"

示例 http://jsfiddle.net/f42xw/

这篇关于从knockout.js 中的下拉列表更新对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆