I want to set the id of an input box which am getting in for loop using knockout. How do I set this?

<div data-bind="foreach: testData">
    <span class="cld" data-bind="text:flid"/>
    <input type="hidden" id="hid_freq_{flid}" name="hid_freq_{flid}">
 </div>  

In the above code i am getting flid as data in a span I want to use that same data as an id for an input box, so please let me know whether it is possible or not.

有帮助吗?

解决方案

You need to use the attr binding to set any attributes like id or name

<span class="cld" data-bind="text:flid"/>
<input type="hidden" 
   data-bind="attr: { id: 'hid_freq_' + flid(), name: 'hid_freq_' + flid() }">

Note: you only need to write flid() only if your flid property is a ko.observable otherwise you just need 'hid_freq_' + flid.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top