如何使用隐藏的输入字段来存储博客文章的标签集
我有一些稍微时髦的用户界面,用于为博客文章输入标签:当标签被输入到输入字段中时,它们被包装到跨度中,通过将它们包围在一个风格化的框中,使它们看起来很漂亮,最终结果是这样的: http://forr.st/posts/OLs/original
现在,这个输入字段(称之为字段 1) 不是提交给控制器的表单的一部分(顺便说一句,我正在使用 RoR),原因有两个:除了实际标签之外,它还包含无关的 html 标签;另外,如果它是表单的一部分,则按 Enter 键将提交表单,而不是触发将输入的标记包装到跨度中的 js。
因此,我所做的是,当输入每个标签时,我将其值(通过 js)复制到隐藏的输入字段,该输入字段是标签输入表单的一部分,并且在提交时将仅包含标签值而不包含其他内容。问题是:我应该使用什么作为分隔符来分隔隐藏输入字段中的标签。目前我正在使用 ';'但如果标签本身包含 ;那会引起问题。
我也愿意接受有关如何跟踪输入“字段 1”的标签的一般方法的建议,
非常感谢,
I have some slightly funky UI for inputting tags for a blog post: as tags are entered into an input field they are wrapped into spans that make them look nice by surrounding them in a stylized box, the end result comes out to be something like this:
http://forr.st/posts/OLs/original
Now, this input field (call it field 1)is not part of the form that gets submitted to the controller (I'm using RoR btw) for two reasons: it contains extraneous html tags, besides the actual tags; also if it was part of the form pressing enter would submit the form instead of triggering the js that wraps the entered tag into a span.
So what I'm doing is when each tag is entered, I copy its value (via js) to a hidden input field that IS part of the tag entry form, and when submitted would contain only the tag values and nothing else. The question is: What should I use as delimiter to separate the tags in the hidden input field. Currently I'm using ';' but if a tag itself contains ; that'd cause problems.
I'm also open to suggestions about the general method of how to keep track of the tags entered into 'field 1'
Thanks a lot,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议只为每个标签添加一个隐藏输入。
然后在 Rails
post.rb
中,我对 tokenInput jQuery 插件使用类似的方法。但就我而言,我已将其放入表单中。我通过捕获按键事件并阻止该输入解决了您提到的问题,并且忽略了搜索输入值。
我真正喜欢将其保留在表单中的一件事是之后如何管理它。我将隐藏标签、名称和删除“x”放在一个范围中(就像您提到的那样),然后在单击“x”时删除此标签。我喜欢这个,因为名称和隐藏标签同时被删除。
只是另一个提示。如果可以,请在隐藏字段中传递 tag_id。这样你就不必添加标签属性添加全部:
。
I would recommend just adding a hidden input for each tag.
then in rails
post.rb
I use a similar method with the tokenInput jQuery plugin. But in my case I've placed it inside the form. I solved the problems that you mentioned by capturing the keypress event and preventing it for that input and I ignore the search input value.
The one thing that I really like about keeping it inside the form is how it is managed afterward. I place the hidden tag, name, and a remove 'x' in a span (like you mentioned) and then just remove this tag when the 'x' is clicked. I like this because the name and the hidden_tag are removed at the same time.
Just one other tip. If you can, pass the tag_id in the hidden field. This way you don't have to add the tags attribute add all:
<input type="hidden" name="post[tag_ids][]" value="tag_name" />
.