【问题标题】:How do I use visible bindings with radio button observable parameter?如何使用带有单选按钮可观察参数的可见绑定?
【发布时间】:2011-08-26 21:06:30
【问题描述】:

在呈现此页面时选择了正确的单选按钮,但在选择“sendemail”或“sms”单选按钮值时应该会出现其他输入,对吧?

$(function () {
    var rbViewModel = {
        qrType: ko.observable('plaintext')
    };
    ko.applyBindings(rbViewModel);
});

然后是我的单选按钮

<input type="radio" name="txtType" value="plaintext" data-bind="checked: qrType" />Plaintext
<input type="radio" name="txtType" value="sendemail" data-bind="checked: qrType" />Send E-mail
<input type="radio" name="txtType" value="sms" data-bind="checked: qrType" />SMS

我的意见:

<div data-bind="visible: qrType == 'sendemail'"><input type="text" id="txtEmailSubject" placeholder="E-mail subject" /></div>
<div data-bind="visible: qrType == 'sendemail'"><input type="text" id="txtEmailBody" placeholder="E-mail body" /></div>
<div data-bind="visible: qrType == 'sms'"><input type="text" id="txtSmsMsg" placeholder="SMS body" /></div>

div 元素的属性有问题吗?我认为函数可以在每个 KnockoutJS documentation 的可见绑定中使用,如下所示:

data-bind="visible: qrType=='sendemail'"

【问题讨论】:

    标签: javascript data-binding knockout.js


    【解决方案1】:

    当你在数据绑定属性的表达式中使用 observable 时,你需要用 () 来引用它。

    他们需要看起来像:visible: qrType() === 'sendemail'

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-25
      • 2013-05-13
      • 2017-09-05
      • 2016-02-29
      • 2015-03-05
      • 1970-01-01
      • 2012-07-29
      相关资源
      最近更新 更多