我正在使用Knockout JS。而在下拉菜單的訂閱功能中。我需要獲取所選下拉列表的索引。Knockout Js:獲取表格行下拉訂閱的索引值
這裏爲下拉是表格內(即的foreach TR)
HTML:
<table>
<tbody data-bind="foreach: Rows">
<tr>
<td>
<select data-bind="options: Materials, value: selectedMaterial,attr:{index:$(index)}"></select>
</td>
</tr>
</table>
腳本:
this.selectedMaterial.subscribe(function(data){
// I need to get the index value of the selectedMaterial
// i try to get like following code but its not working
var k =$(this).attr("index");
});
你想在索引後做什麼?你爲什麼不從材質集合'Materials.indexOf(data)'獲取索引? – nemesv