1

我有一个技能列表,当用户检查技能时,如果选中,则会显示多年的输入。我的问题是,如果用户输入了一年,但随后取消检查技能,那么多年的值仍然与输入中的文本一起保存在 observable 中。我不确定是否需要使用事件处理程序、订阅函数或仅使用 ko 计算函数清除值。

http://jsfiddle.net/bjk964/CQ2CG/2/

<div data-bind="foreach: ApplicationSkills">                
    <p>
        <span data-bind="text: Skill_SkillTypeName"></span>
        <input type="checkbox" data-bind="checked: Skill_Checked" />
        <span data-bind="visible: Skill_Checked" class="years">Years:</span>
        <input type="text" data-bind="value: Skill_Years, visible: Skill_Checked" />
    </p>                
</div>

var Application = function () {
    this.ApplicationSkills = ko.observableArray();
};

var ApplicationSkill = function () {
    var self = this;
    this.Skill_SkillTypeID = ko.observable(0);
    this.Skill_Years = ko.observable().extend({ maxLength:2, number:true });
    this.Skill_Checked = ko.observable(false);
    this.Skill_SkillTypeName = ko.observable();
};

var vm = new Application();
ko.applyBindings(vm);

var skill = new ApplicationSkill()
skill.Skill_SkillTypeID(1);
skill.Skill_SkillTypeName('Data Entry');

vm.ApplicationSkills.push(skill); 
4

1 回答 1

2

我会做一个计算:

var skillYears = ko.observable();
this.Skill_Years = ko.computed({
    read: function () { return this.Skill_Checked() ? skillYears() : undefined; },
    write: skillYears
}, this);
于 2013-05-24T16:21:58.810 回答