简体   繁体   English

如何在KnockoutJS中正确使用可见绑定

[英]How to properly use visible binding in KnockoutJS

I want the .controls div to be visible only when the value in the textbox is _OTHER . 我希望.controls div仅在文本框中的值为_OTHER Here's the code I'm using: 这是我正在使用的代码:

<div class="controls" data-bind="
    visible: title == '_OTHER'
">            
    @Generic.Selection <i data-bind="text: $index() + 1"></i>
    <input type="text" id="inputAnswerContent" data-bind="value: title" />
    <a href="#" class="btn btn-small" data-bind="
        visible: $parent.requireOfferedAnswer, 
        click: $parent.addAnswer
    ">
        <i class="icon-plus"></i>
    </a>
</div>

However, it's visible for all values other than _OTHER . 但是,对于_OTHER以外的所有其他值均可见。

You want to use `visible: title() == '_OTHER' (with parentheses). 您想使用`visible:title()=='_OTHER'(带括号)。

Simplified example ( jsfiddle ): 简化示例( jsfiddle ):

HTML 的HTML

<div class="controls" data-bind="visible: title() == '_OTHER'">            
hello world
</div>
<input type="text" data-bind="value : title"></input>

JS JS

function ViewModel() {
    this.title = ko.observable();
}

var vm = new ViewModel();

ko.applyBindings(vm);

Type _OTHER in the text box to make hello world appear. 在文本框中键入_OTHER ,使hello world出现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM