使用带有前缀的基因敲除js设置id属性 [英] Setting the id attribute with knockoutjs including a prefix
问题描述
我正在使用KnockoutJS遍历一个对象,如下所示:
I'm using KnockoutJS to iterate over an object, like this:
现在一切正常.但是我有一个问题是它将button
的id
设置为一个数字.所以看起来像这样:
Now this all works. But the problem i have is that it sets the id
of the button
to just a number. So it looks like this:
<button id="1">Button 1</button>
<button id="3">Button 2</button>
<button id="8">Button 3</button>
所以我试图在'Id'属性的前面加上一个前缀,如下所示:
So i tried to put a prefix in front of the the 'Id' property, like so:
<div data-bind="foreach:Items">
<button data-bind="text: Name, attr: {'id': 'myprefix_' + Id}"></button>
</div>
但这似乎不起作用.当我这样做的时候,我的id
充满了一些可观察到的功能……
But that doesn't seem to be working. My id
gets filled with some Knockout observable function when i do it like that...
所以我的问题是,当我指定字段的id
属性时如何添加前缀?
So my question is, how can i add a prefix when i specify the id
attribute of a field?
推荐答案
如果Id
是可观察到的,则必须对其展开":'myprefix_' + Id()
.
If Id
is an observable, you must "unwrap" it: 'myprefix_' + Id()
.
这篇关于使用带有前缀的基因敲除js设置id属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!