使用带有前缀的基因敲除js设置id属性


76

我正在使用KnockoutJS遍历一个对象,如下所示:

现在,这一切正常。但我的问题是,它设置idbutton只是一个数字。所以看起来像这样:

<button id="1">Button 1</button>
<button id="3">Button 2</button>
<button id="8">Button 3</button>

因此,我尝试在“ Id”属性前放置一个前缀,如下所示:

<div data-bind="foreach:Items">
    <button data-bind="text: Name, attr: {'id': 'myprefix_' + Id}"></button>
</div>

但这似乎不起作用。id当我这样做时,我就充满了一些淘汰赛可观察的功能...

所以我的问题是,当我指定id字段的属性时如何添加前缀?

Answers:



68

今天实际使用的是-解开我必须做的可观察的事情:

<button data-bind="attr: { id: 'prefix_' + $index() }"> Send </button>

希望这可以帮助。


29

我认为最好使用$ index为例

<div data-bind="foreach:Items">
    <button data-bind="text: Name, attr: {id: 'myprefix_' + $index() }"></button>
</div>

5
我至少在我的特定情况下发现我必须使用“ $ index()”,而不仅仅是“ $ Index”,因此我提出了异议。
Mark Meuer

-1

       <img data-bind="event: {click: $root.afficherDetailmembreFamille}" src="ucc/gestion_Famille/images/arbre-fleche-off.png" />

          <label data-bind=" text: nom"></label>
          <label data-bind=" text: prenom, click: $root.afficherDetailmembreFamille"></label>
  <br>

   <div data-bind="attr: {'id': 'DivMembreFamille'+id}" style="margin-left: 40px; display: none;">
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.