引用自定义html id的knockout observable

时间:2014-05-01 15:18:33

标签: knockout.js

我有一个foreach knockout循环,我想在循环中为每个复选框设置我的observable名称的id。

<tbody data-bind="foreach: FormatVendorRules">
                    <tr class="h4">
                        <td data-bind="text: SubCategoryCode"></td>
                        <td>
                            <div class="onoffswitch">
                                <input name="validationCheckBox" type="checkbox" 
                                name="onoffswitch" class="onoffswitch-checkbox" 
                                id="myId+"text: SubCategoryCode"" data-bind="checked: IsEnabled" /> //This is where I want to define the id
                                <label class="onoffswitch-label" for="myId+"text: SubCategoryCode"">
                                    <div class="onoffswitch-inner"></div>
                                    <div class="onoffswitch-switch"></div>
                                </label>
                            </div>
                        </td>
                    </tr>
                </tbody>

所以id应该是

  • myIdPrice
  • myIdValue
  • myIdSku
  • ...... ECT

1 个答案:

答案 0 :(得分:3)

您需要使用attr binding来设置id属性(或任何其他属性):

data-bind="checked: IsEnabled, attr: { id: 'myId' + SubCategoryCode() }"

注意:如果此属性为ko.observable,则只需()末尾的SubCategoryCode()。但是,如果您的SubCategoryCode是常规属性,则只需编写: attr: { id: 'myId' + SubCategoryCode }