Tag-It jquery插件上出现多个'createTag'错误

时间:2015-09-29 10:02:11

标签: javascript jquery jquery-plugins tag-it

我正在使用https://github.com/aehlke/tag-it

中的tag-it jquery插件
var ip_elem = $('#my-tags');

ip_elem.tagit({
    removeConfirmation: false,
    caseSensitive: false,
    allowDuplicates: false,
    allowSpaces: true,
    readOnly: false,
    tagLimit: null,
    singleField: false,
    singleFieldDelimiter: ',',
    singleFieldNode: null,
    tabIndex: null,
    placeholderText:"Enter Tags"
});

现在,我想使用createTag方法以编程方式添加更多标记。

var newTags = ["javascript","php","ruby","python"];

for(var k=0;k<newTags.length;k++){
    ip_elem.tagit("createTag", newTags[k]);
}

它显示输入中的第一个标记,但第二个标记显示:Uncaught Error: cannot call methods on tagit prior to initialization; attempted to call method 'createTag'。它会停止,现在不会在输入框中添加任何标签。因此,最后,只形成第一个标签(在本例中为javascript

这里出了什么问题?

1 个答案:

答案 0 :(得分:1)

看看我在Fiddle中创建的这个例子:

在小提琴示例中,它会动态创建onload tag,如果您尝试输入具有相同名称的var newTags = ["javascript","php","ruby","python"]; var addEvent = function(text) { $('#events_container').append(text + '<br>'); }; $(function(){ var ip_elem = $('#myTags'); ip_elem.tagit({ removeConfirmation: false, caseSensitive: false, allowDuplicates: false, allowSpaces: true, readOnly: false, tagLimit: null, singleField: false, singleFieldDelimiter: ',', singleFieldNode: null, tabIndex: null, placeholderText:"Enter Tags" }); ip_elem.tagit({ availableTags: newTags, beforeTagAdded: function(evt, ui) { if (!ui.duringInitialization) { addEvent('Before Tag Added: ' + ip_elem.tagit('tagLabel', ui.tag)); } }, afterTagAdded: function(evt, ui) { if (!ui.duringInitialization) { addEvent('After Tag Added: ' + ip_elem.tagit('tagLabel', ui.tag)); } }, beforeTagRemoved: function(evt, ui) { addEvent('Before Tag Removed: ' + ip_elem.tagit('tagLabel', ui.tag)); }, afterTagRemoved: function(evt, ui) { addEvent('After Tag Removed: ' + ip_elem.tagit('tagLabel', ui.tag)); }, onTagClicked: function(evt, ui) { addEvent('On Tag Clicked: ' + ip_elem.tagit('tagLabel', ui.tag)); }, onTagExists: function(evt, ui) { addEvent('On Tag Exists: ' + ip_elem.tagit('tagLabel', ui.existingTag)); } }); for(var k=0;k<newTags.length;k++) { ip_elem.tagit("createTag", newTags[k]); } }); ,则不会让您添加它。< / p>

希望这会帮助你,它会有事件向你展示页面上实际发生的事情。

class MyController extends AppController {
    public function someAction() {
        //........
        $event = new CakeEvent('Controller.MyController.example', $this, array(
            'data' => $someData
        ));
        $this->getEventManager()->dispatch($event);
        //.........
    }
}

JSFIDDLE EXAMPLE