如何使用knockout.js
設置焦點被綁定到陣列中的模板創建一個元素?knockout.js將焦點設置在一個模板
我有綁定到一個表中,其中每一行是一組輸入元件,以允許陣列元素的屬性要被編輯的可觀察陣列。在底部是一個"Add"
按鈕,將一個新元素插入到陣列中,創建輸入字段的一個新行。
我試圖做的是有"Add"
按鈕被按下後,設置爲第一個新創建的輸入域的焦點。
HTML:
的Javascript:
function Attribute(id, name, description, hardcoded) {
var self=this;
self.AttributeID=ko.observable(id || 0);
self.Name=name || '';
self.Description=description || '';
self.HardCoded=hardcoded || false;
self.nameFocus = true;
}
function AttributeSchema(attributeArray) {
var self=this;
// Properties
self.Attributes=ko.observableArray(attributeArray);
// Operations
self.addAttribute=function() {
self.Attributes.push(new Attribute());
};
self.removeAttribute=function() {
self.Attributes.remove(this);
};
}
var vmSchema=new AttributeSchema(
[
new Attribute(5, 'FirstName', 'First Name', true),
new Attribute(6, 'LastName', 'Last Name', true),
new Attribute(7, 'Blah', 'Blah', false)
]
);
ko.applyBindings(vmSchema);
非常感謝,完美的答案! Knockout.js岩石! – Richard 2012-02-26 11:48:03