淘汰赛中的UniqueID绑定

时间:2011-08-31 16:08:56

标签: knockout.js

我正在尝试弄清楚如何复制元素ID的Unique Name绑定功能(让pretty validation正常工作)

ko.bindingHandlers.uniqueID = {
   'init': function (element, valueAccessor) {
        if (valueAccessor()) {
            element.ID = "ko_unique_" + (++ko.bindingHandlers['uniqueName'].currentIndex);
        }
    }
};

1 个答案:

答案 0 :(得分:4)

uniqueName的淘汰源是

ko.bindingHandlers['uniqueName'] = {
    'init': function (element, valueAccessor) {
        if (valueAccessor()) {
            element.name = "ko_unique_" + (++ko.bindingHandlers['uniqueName'].currentIndex);

            // Workaround IE 6 issue - http://www.matts411.com/post/setting_the_name_attribute_in_ie_dom/
            if (ko.utils.isIe6)
                element.mergeAttributes(document.createElement("<input name='" + element.name + "'/>"), false);
        }
    }
};
ko.bindingHandlers['uniqueName'].currentIndex = 0;

所以我认为你应该有这个(注意ID应该是id,删除IE6的东西,并将uniqueName更改为uniqueId)

ko.bindingHandlers['uniqueId'] = {
    'init': function (element, valueAccessor) {
        if (valueAccessor()) {
            element.id = "ko_unique_" + (++ko.bindingHandlers['uniqueId'].currentIndex);           
        }
    }
};
ko.bindingHandlers['uniqueId'].currentIndex = 0;