使用带HTML按钮的Knockout

Using Knockout with an HTML button

本文关键字:Knockout 按钮 HTML      更新时间:2023-09-26

我以以下方式成功地使用了敲除:

<div id="Options" data-bind="foreach: Options">
        <button type="button" data-bind="css: { selected: IsSelected }, enable: $parent.allow, click: $parent.select"><img src="/path/to/img.png"/></button>
    </div>

现在,我正试图用以下内容将按钮更改为图像:

<div id="Options" data-bind="foreach: Options">
        <input type="image" data-bind="css: { selected: IsSelected }, attr:{src:/path/to/img.png}, enable: $parent.allow, click: $parent.select" />
    </div>

我的目标是使用淘汰赛将每个选项与自己的图像配对。

按钮没有加载第二个选项,所以我认为语法不正确。知道为什么它不起作用吗?

设置图像源的方式是错误的。您试图将其设置为字符串的值,但需要将其写成字符串文字(即带引号)。您之所以能够在其他绑定中不设置绑定,是因为它们是视图模型的属性。

其中任何一个都应该有效:

<input type="image" data-bind="css: { selected: IsSelected }, attr:{src:'/path/to/img.png'}, enable: $parent.allow, click: $parent.select" />
<input type="image" src="/path/to/img.png" data-bind="css: { selected: IsSelected }, enable: $parent.allow, click: $parent.select" />