[英]applying knockout if and options binding together in one data-bind
I am trying to move an if statement into a data-bind but i am running into an error stating: 我试图将一个if语句移动到数据绑定,但我遇到一个错误说明:
Multiple bindings (if and options) are trying to control descendant bindings of the same element.
多个绑定(if和options)试图控制同一元素的后代绑定。
what i am trying to do is to control the visibility of a dropdown list so it doesn't appear based on the if statement condition. 我想要做的是控制下拉列表的可见性,以便它不会基于if语句条件出现。 I tried to use the visible bind but it only removed the dropdown elements not the actual dropdown.
我试图使用可见的绑定但它只删除了下拉元素而不是实际的下拉列表。
This is what i am currently attempting: 这就是我目前正在尝试的:
<select id="IdField" name="Id" data-placeholder="Select an item" data-bind="if: items().length > 0, options: items(), optionsText: 'Name', optionsValue: 'Id', value: DdlSelectedValue, event: { change: selectChanged }">
</select>
This is what my original code looked like: 这是我的原始代码:
<!-- ko if: items().length > 0 -->
<select id="IdField" name="Id" data-placeholder="Select an item" data-bind="options: items(), optionsText: 'Name', optionsValue: 'Id', value: DdlSelectedValue, event: { change: selectChanged }">
</select>
<!--/ko-->
is there a way i could move the if statement into the data-bind with the options? 有没有办法可以将if语句移动到带有选项的数据绑定中?
What you have is the correct way if you want the select
to actually be absent from the page when items().length
is zero. 你有什么是正确的方式,如果你想
select
实际上是不存在的,从页面时items().length
为零。
Alternately, you could use visible
, which is of course slightly different (when items().length
is 0, the select
will be there, it'll just be hidden): 或者,您可以使用
visible
,当然稍微不同(当items().length
为0时, select
将在那里,它只是被隐藏):
<select id="IdField" name="Id" data-placeholder="Select an item" data-bind="visible: items().length > 0, options: items(), optionsText: 'Name', optionsValue: 'Id', value: DdlSelectedValue, event: { change: selectChanged }">
</select>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.