2014-05-01 47 views
0

我有一个foreach挖空循环,我想在每个复选框中为我的observable在循环中创建的名称设置id。引用自定义html 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

回答

3

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

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

注意:您只需要()SubCategoryCode()结束时,如果这个属性是ko.observable。但如果你的SubCategoryCode是一个普通的属性,你只需要写: attr: { id: 'myId' + SubCategoryCode }

+0

非常好,这工作得很好。 –