简体   繁体   English

jQuery - Facebook 样式标记,带有字符串和隐藏字段正则表达式。 自动完成

[英]jQuery - Facebook style tagging with string and hidden fields regex. Autocomplete

I would like to create a similar system to the facebook status update using jQuery.我想使用 jQuery 创建与 facebook 状态更新类似的系统。

If you take a look at the guts of it, just a few elements below the What's on your mind?如果您仔细看一下它的内容, What's on your mind? textarea tag, there is a hidden field... textarea 标签,有一个隐藏字段...

<input type="hidden" autocomplete="off" class="mentionsHidden" name="xhpc_message" value="">

As you type a status into the textarea field, the value of this hidden field changes to match the status.当您在 textarea 字段中键入状态时,此隐藏字段的值会更改以匹配状态。 However, if you tag someone into the status, the hidden field displays something like @[uid:User Name] in place of the users name, so that it can be turned into a hyperlink later on.但是,如果您将某人标记到状态中,隐藏字段会显示类似@[uid:User Name]的内容来代替用户名,以便以后可以将其转换为超链接。

I have tried to recreate something similar myself, not with users, but with tags.我试图自己重新创建类似的东西,不是用用户,而是用标签。 http://jsfiddle.net/nickjackson/qh3QP/ http://jsfiddle.net/nickjackson/qh3QP/

When I type #fun into the first text area field, the second text area displays #[tag:fun] and the #fun tag becomes fun .当我在第一个文本区域字段中键入#fun时,第二个文本区域显示#[tag:fun]并且#fun标签变为fun This is all correct.这都是正确的。 However, when I then type something else, the square bracket disappears because the # tag is no longer in the status field.但是,当我随后键入其他内容时,方括号会消失,因为 # 标记不再位于状态字段中。 How can I solve this?我该如何解决这个问题?

Ultimately, How can I create a textarea and hidden field combo, that works similarly to the facebook status update, and allows the use of tags?最终,如何创建文本区域和隐藏字段组合,其工作方式类似于 facebook 状态更新,并允许使用标签?

I used 3 fields - one to type in, one to store the tag stuff in, and one to display the text.我使用了 3 个字段 - 一个用于输入,一个用于存储tag内容,一个用于显示文本。 The type-in field is invisible but on top of display field.输入字段是不可见的,但在显示字段的顶部。 It should be pretty self-explanatory, but ask any questions you may have - and no, I have no quick solution for there being no cursor.这应该是不言自明的,但请提出您可能有的任何问题 - 不,我没有快速解决方案,因为没有 cursor。

http://jsfiddle.net/qh3QP/10/ http://jsfiddle.net/qh3QP/10/

As Henry Garle stated, you are unable to have more than one encoded tag at a time in hidden-field , (unless you paste text containing multiple tags in the status-field ), because the code needs to find tags in status-field in order to create encoded versions of them in hidden-field .正如 Henry Garle 所说,您一次不能在hidden-field中拥有多个编码标签(除非您在status-field中粘贴包含多个标签的文本),因为代码需要在status-field中找到标签为了在hidden-field中创建它们的编码版本。 Since it only preserves the values of the tags in status-field after doing this, the next time the code runs (which is after a keypress event), it treats said values as regular text, which it simply copies in to hidden-field , since it is not able to identify them as values of tags.由于在执行此操作后它只保留status-field中标签的值,因此下次代码运行时(在按键事件之后),它将所述值视为常规文本,它只是将其复制到hidden-field ,因为它无法将它们识别为标签的值。

The fix is simple: since presumably only one tag can be created at a time, all you have to do is modify your code to solely target the first appearing tag in status-field .修复很简单:因为可能一次只能创建一个标签,您所要做的就是修改您的代码以仅针对status-field中第一个出现的标签。 Since no other tags will be targeted, their encoded versions in hidden-field will be preserved.由于不会针对其他标签,因此它们在hidden-field中的编码版本将被保留。

Before you of implement that fix, however, you should consider maintaining an object for each tag that contains its value and positional data instead.但是,在实施该修复之前,您应该考虑为每个包含其值和位置数据的标签维护一个 object。 Such an approach separates the internal and external representations of tags, allows tags to be located quicker, and provides greater flexibility in how tags are to be ultimately formatted.这种方法分离了标签的内部和外部表示,允许更快地定位标签,并在如何最终格式化标签方面提供更大的灵活性。

Mentionator is an existing, robust solution which takes the approach in providing the functionality you are trying to recreate. Mentionator是一种现有的、强大的解决方案,它采用提供您尝试重新创建的功能的方法。 Considering it is well-structured, easy to follow, and copiously commented, it should be of use to you as either out-of-the box solution or reference material to cite as you roll out your own.考虑到它结构良好、易于理解且评论丰富,它应该对您有用,既可以作为开箱即用的解决方案,也可以作为参考材料,在您推出自己的解决方案时引用。 It is maintained by yours truly:).它是由你真正维护的:)。

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

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