django +用jquery标记(django + tagging with jquery)

编程入门 行业动态 更新时间:2024-10-26 16:26:34
django +用jquery标记(django + tagging with jquery)

我想使用jQuery并为用户创建标记界面。 类似于StackOverflow中的用户可以为他们提出的问题类型添加标记。 我正在使用jQuery和tagit工作:

http://jquery.webspirited.com/2011/02/jquery-tagit-a-jquery-tagging-plugin/

图书馆的新位置:

https://github.com/hailwood/jQuery-Tagit

问题是,如果用户在表单的另一部分中出现错误并单击“提交”,则表单将重新加载并显示错误消息,并且所有标记都已消失。 有没有一种简单的方法来获取Django中的标记?

[编辑]

根据Hailwood在下面的回复来试试这个......

<ul name="event_tag" class="tags"> <li class="tagit-choice" tagvalue="3"> Dog <a class="tagit-close">x</a> </li> </ul>

但是,当我加载页面加载时,没有加载该特定标记? 似乎清除了ul标签,然后加载了其他信息。 我加载页面时看不到它。

我也按以下方式尝试:

<ul name="event_tag" class="tags"> <li data-value="3">Dog</li> </ul>

当我尝试这种方式时,它会出现一秒然后消失......

[编辑2]

找到了我的问题的解决方案。 正如Hailwood建议的那样,我们可以按照以下方式创建li :

<ul name="event_tag" class="tags"> <li data-value="3">Dog</li> </ul>

它不适合我的原因是因为我有初始值:

$.getJSON("ajaxrequest.json", function(data) { $(".tags").tagit("fill", data); });

对我来说问题是onload,它全都消失了。 原因是因为fill 。 当我们用add替换fill时: $(".tags").tagit("add", data); ,然后它的工作原理。

I want to use jQuery and create a tagging interface for users. Similar to how users in StackOverflow can add tags for the type of question they are asking. I'm getting it to work using jQuery and tagit:

http://jquery.webspirited.com/2011/02/jquery-tagit-a-jquery-tagging-plugin/

New location for the library:

https://github.com/hailwood/jQuery-Tagit

The problem is that say the user has an error in another part of the form and clicks submit, the form reloads with the error message and all the tags are gone. Is there a simplar way to get tagging in Django?

[EDIT]

Trying this based on Hailwood's response below...

<ul name="event_tag" class="tags"> <li class="tagit-choice" tagvalue="3"> Dog <a class="tagit-close">x</a> </li> </ul>

However, when I load the page loads, that particular tag is not loaded? It seems like the ul tag is cleared and then there is other information loaded in it. I do not see it when I load the page.

I also tried as per below:

<ul name="event_tag" class="tags"> <li data-value="3">Dog</li> </ul>

When I try this way, it appears for a second and then it disappears...

[EDIT 2]

Found the solution to my problem. As Hailwood suggested, programmatically we can create the li's as per below:

<ul name="event_tag" class="tags"> <li data-value="3">Dog</li> </ul>

Reason it wasn't working for me was because I had for initial values:

$.getJSON("ajaxrequest.json", function(data) { $(".tags").tagit("fill", data); });

Problem for me was that onload, it was all disappearing. Reason for this is because of fill. When we replace fill with add as per: $(".tags").tagit("add", data);, then it works.

最满意答案

作为你提到的tagit插件的创建者,我想我可以帮助你。

从我可以看到它看起来你正在使用它作为表单的一部分,当你点击提交你验证服务器端的表单,如果发生错误,你重新加载页面与表格值?

我建议你做的是启用隐藏的选择选项( select: true )。

提交表单后,您将获得所选标签的列表。

然后,如果您重新加载页面,只需将标签重新加载到列表中<li> s。

(请注意我不懂Python,所以以下是伪代码)

if(form_values.tags) for(tag in form_values.tags) print '<li data-value=" '+tag.value+' "> '+tag.label+' </li>'; endfor; endif;

*你也可以将标签传递给initialTags选项,但这需要输出javascript,这比上面的方法更整洁:)

Being the creator the tagit plugin you mentioned I guess I could help you out.

From what I can see it looks like you are using it as part of a form, and when you click submit you validate the form on the server side, if an error occurs you reload the page with the form values in tow?

What I would suggest you do is enable the hidden select option (select: true).

When the form is submitted you will get a list of the selected tags.

Then, if you reload the page simply load the tags back into the list as <li>s.

(Please note I do not know Python, so the following is pseudo code)

if(form_values.tags) for(tag in form_values.tags) print '<li data-value=" '+tag.value+' "> '+tag.label+' </li>'; endfor; endif;

*You could also pass the tags to the initialTags option, but that would require outputting javascript, which imo is less tidy than the above method :)

更多推荐

本文发布于:2023-07-16 12:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1128531.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:标记   django   jquery   tagging

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!