KnockoutJS更改复选框更改事件的值和输入文本

时间:2013-05-24 16:04:55

标签: jquery knockout.js

我有一份技能列表,当用户检查技能时,如果选中,则会显示多年的输入。我的问题是,如果用户输入一年但是然后取消检查技能,则年份值仍然会与输入中的文本一起保存在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); 

1 个答案:

答案 0 :(得分:2)

我将其作为计算机执行:

var skillYears = ko.observable();
this.Skill_Years = ko.computed({
    read: function () { return this.Skill_Checked() ? skillYears() : undefined; },
    write: skillYears
}, this);