Knockout.js 敲除不在单选按钮上设置初始真值

Knockout.js 敲除不在单选按钮上设置初始真值,knockout.js,Knockout.js,names+复选框的单向绑定可以正常工作,但它最初对单选按钮employeeTypeA不起作用,尽管它在viewmodel中的值为true。html将单选按钮显示为未设置,这是为什么 <script type="text/javascript"> $(function() { var PersonViewModel = function() { this.first

names+复选框的单向绑定可以正常工作,但它最初对单选按钮employeeTypeA不起作用,尽管它在viewmodel中的值为true。html将单选按钮显示为未设置,这是为什么

   <script type="text/javascript">

        $(function()
        {
            var PersonViewModel = function()
            {
                this.firstName = ko.observable('Lisa');
                this.lastName = ko.observable('T');
                this.isCustomer = ko.observable(true);
                this.employeeTypeA = ko.observable(true);
                this.employeeTypeB = ko.observable(false);
            };

            var personViewModel = new PersonViewModel();
            ko.applyBindings(personViewModel, $('data').get(0));
        });
    </script>

    <div id="data">
        <span data-bind="text: firstName"></span>
        <span data-bind="text: lastName"></span>
        <input type="checkbox" data-bind="checked: isCustomer" title="Is a customer" />
        <input name="x" type="radio" data-bind="checked: employeeTypeA" title="Employee type A" />
        <input name="x" type="radio" data-bind="checked: employeeTypeB" title="Employee type B" />
    </div>

$(函数()
{
var PersonViewModel=函数()
{
this.firstName=ko.observable('Lisa');
this.lastName=ko.observable('T');
this.isCustomer=ko.可观察(真);
this.employeeTypeA=ko.可观察(真);
this.employeeTypeB=ko.可观察(假);
};
var personViewModel=新personViewModel();
应用绑定(personViewModel,$('data').get(0));
});

选中的
绑定对单选按钮的作用与文档中的不同:

对于单选按钮,当且仅当参数值等于单选按钮节点的
属性时,KO将设置要检查的元素

因此,您需要将
PersonViewModel
更改为如下内容:

var PersonViewModel = function()
{
    this.firstName = ko.observable('Lisa');
    this.lastName = ko.observable('T');
    this.isCustomer = ko.observable(true);
    this.employeeType = ko.observable('TypeB');                
};
和您的单选按钮:

<input name="x" type="radio" data-bind="checked: employeeType" 
       value="TypeA" title="Employee type A" />
<input name="x" type="radio" data-bind="checked: employeeType" 
       value="TypeB" title="Employee type B" />
在这种情况下,您还需要在单选按钮上添加
属性


演示

我查看了knockoutJS站点,并查看了教程:wantsSpam:ko.observable(true),但我没有看到wantsSpam绑定到可见性属性。。。不是直接按下单选按钮。正如你也知道ASP.NET MVC,在服务器端VIEWDATM中考虑这个布尔到字符串“转换器”不是一个更好的主意吗?当我将您的最后一个代码块放入visual studio html文件时,我收到了两个错误/警告:可疑使用此运算符,并且并非所有代码路径都返回值。我的另一个问题是,为什么要使用“this.employeeTypeA()”而不是“this.employeeTypeA”。就我的javascript初学者知识而言,employeeType是一个属性,但您像函数一样调用吗?这不是批评,只是一个奇怪的问题:)回答你的问题:1。如何将其与ASP.NETMVC连接是完全不同的问题,您应该在单独的问题中提问。2.关于警告:计算结束时缺少一个
返回'
。可疑使用
:在这种情况下它会起作用,但有多种方法可以处理它,如引入等。关于“this.employeeTypeA()”调用:
ko.observable
返回一个函数,因此
employeeTypeA
是一个函数,您需要使用“this.employeeTypeA()”调用它以获取其值。好的,谢谢。我见过使用var self=this的样本;人们在浏览谷歌时似乎真的用得不一样了。大约3。)它在没有()的情况下也可以工作:好的,我会问一个新问题。
this.employeeTypeA = ko.observable(false);
this.employeeTypeB = ko.observable(true);
this.employeeType = ko.computed(function()
{
     if (this.employeeTypeA())
        return 'TypeA';
     if (this.employeeTypeB())
        return 'TypeB';
},this);