简体   繁体   English

如何为jQuery实现标签插件

[英]How to implement a tagging plugin for jQuery

Goal: To implement a jQuery plugin for my rails app (or write one myself, if necessary) that creates a "box" around text after a delimiter is typed. 目标:为我的rails应用程序实现jQuery插件(或在必要时自己写一个),在键入定界符后在文本周围创建一个“框”。

Example: With tagging on SO, the user begins typing a tag, then selects one from the drop-down list provided. 示例:在SO上加标签后,用户开始输入标签,然后从提供的下拉列表中选择一个。 The input field recognizes that a tag has been selected, puts a space and then is ready for the next tag. 输入字段识别出已选择标签,放置一个空格,然后准备下一个标签。 Similarly, I am attempting to use this plugin to put a box around the previously entered tag before moving to to accept the next tag/input. 同样,在尝试接受下一个标签/输入之前,我尝试使用此插件在先前输入的标签周围放置一个框。

The instructions in the README.txt seem simple enough, however I have been receiving a README.txt中的说明似乎很简单,但是我一直在收到

$(".tagbox").tagbox is not a function $(“。tagbox”)。tagbox不是函数

error when debugging my app with firebug. 使用Firebug调试我的应用时出现错误。

Here is what I have in my application.js: 这是我的application.js中的内容:

$(document).ready( function(){

    $('.tagbox').tagbox({
        separator: /\[,]/  // specifying comma separation for <code>tags</code>
    });

});

And here is my _form.html.erb: 这是我的_form.html.erb:

<% form_for @tag do |f| %>
  <%= f.error_messages %>
  <p>
    <%= f.label :name %><br />
    <%= text_field :tag, :name, { :method => :get, :class => "tagbox" } %>
  </p>

  <p><%= f.submit "Submit" %></p>
<% end %>

I have omitted some other code (namely the implementation of an autocomplete plugin) existing within my _form.html.erb and application.js for sake of readability. 为了便于阅读,我省略了_form.html.erb和application.js中存在的其他代码(即,自动完成插件的实现)。 The inclusion or exclusion of this omitted code does not affect the performance of this plugin. 包含或排除此省略的代码不会影响此插件的性能。

I have included all of the necessary files for the tagbox plugin (as well as application.js after all other included JS files) within the javascript_include_tag in my application.html.erb file. 我已经在我的application.html.erb文件的javascript_include_tag中包含了标签箱插件的所有必需文件(以及所有其他包含的JS文件之后的application.js)。

I'm pretty much confused as to why I'd be getting this "not a function" error when jquery.tagbox.js clearly defines the function and is included in the head of my html page in question. 当jquery.tagbox.js清楚地定义了该函数并且包含在所讨论的html页面的开头时,为什么我会收到此“ not a function”错误,我非常困惑。 I've been struggling with this plugin for longer than I'd like to admit, so any help would really be appreciated. 我为此插件苦苦挣扎的时间超过了我想承认的时间,因此我们将不胜感激。 And, of course, I'm open to any other plugins or from-scratch suggestions you may have in mind.. 而且,当然,我愿意接受您可能想到的任何其他插件或从零开始的建议。

This tagbox plugin does not seem to have a wealth of documentation or any currently working examples. 这个标签盒插件似乎没有大量的文档或任何当前有效的示例。

Also to note, I'm trying to avoid using jrails. 另外要注意的是,我试图避免使用jrails。

Thanks for your time 谢谢你的时间

Heres another tagBox plugin I wrote with some additional features: 这是我用其他一些功能编写的另一个tagBox插件:

http://www.geektantra.com/2011/05/jquery-tagbox-plugin/ http://www.geektantra.com/2011/05/jquery-tagbox-plugin/

Just see if it can help. 请看看是否有帮助。

Make sure you are loading the plugin and Jquery in the correct order. 确保以正确的顺序加载插件和Jquery。 Loading the plugin before Jquery can cause this type of error. 在Jquery之前加载插件会导致这种类型的错误。 Also, make sure the plugin is compatable with the JQuery version you are using. 另外,请确保插件与您使用的JQuery版本兼容。 If all of that is correct, I would do a basic JQuery function to ensure that you path to it is correct. 如果所有这些都正确,那么我将执行一个基本的JQuery函数以确保您正确的路径。 Else, use firebug and try to trace down where the load is failing. 否则,请使用Firebug并尝试查找负载失败的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM