如何在Golang中获取<ul>标签的表单值?

huangapple go评论138阅读模式
英文:

How to get form value of <ul> in golang?

问题

在我的用例中,我使用标签tag-it从用户那里获取标签。我以html的&lt;ul&gt;形式获取标签输入。我在服务器端使用golang。

html:

  1. &lt;form class=&quot;comment-form&quot; action=&quot;/add/&quot; method=&quot;POST&quot; enctype=&quot;multipart/form-data&quot;&gt;
  2. &lt;div class=&quot;form-input&quot;&gt;
  3. &lt;label for=&quot;tags_label&quot;&gt;标签&lt;/label&gt;
  4. &lt;ul id=&quot;tags&quot;&gt;
  5. &lt;script type=&quot;text/javascript&quot;&gt;
  6. $(&quot;#myTags&quot;).tagit();
  7. var tagsArray = [&quot;C&quot;, &quot;C++&quot;, &quot;Go&quot;, &quot;Ruby&quot;];
  8. $(&quot;#tags&quot;).tagit({
  9. itemName: &quot;teamId&quot;,
  10. fieldName: &quot;teamName&quot;,
  11. availableTags: tagsArray,
  12. allowSpaces:true,
  13. caseSensitive:false,
  14. removeConfirmation:true,
  15. placeholderText:&quot;标签&quot;,
  16. tagLimit: 5,
  17. allowDuplicates: false,
  18. singleFieldDelimiter: &#39;,&#39;,
  19. onlyAvailableTags: false
  20. });
  21. &lt;/script&gt;
  22. &lt;/ul&gt;
  23. &lt;/div&gt;
  24. &lt;/form&gt;

而在服务器端,我尝试像表单中的其他字段一样获取值,如下所示:

  1. tags := r.FormValue(&quot;tags&quot;)
  2. log.Printf(&quot;Tags : &quot;, tags)

但是它不起作用。有人可以帮我解决这个问题吗?

编辑:
当我检查元素时,我看到以下内容:

  1. &lt;div class=&quot;form-input&quot;&gt;
  2. &lt;label for=&quot;tags_label&quot;&gt;标签&lt;/label&gt;
  3. &lt;ul id=&quot;tags&quot; class=&quot;tagit ui-widget ui-widget-content ui-corner-all&quot;&gt;
  4. &lt;script type=&quot;text/javascript&quot;&gt;
  5. $(&quot;#myTags&quot;).tagit();
  6. var tagsArray = [&quot;C&quot;, &quot;C++&quot;, &quot;Go&quot;, &quot;Ruby&quot;];
  7. $(&quot;#tags&quot;).tagit({
  8. itemName: &quot;teamId&quot;,
  9. fieldName: &quot;teamName&quot;,
  10. availableTags: tagsArray,
  11. allowSpaces:true,
  12. caseSensitive:false,
  13. removeConfirmation:true,
  14. placeholderText:&quot;标签&quot;,
  15. tagLimit: 5,
  16. allowDuplicates: false,
  17. singleFieldDelimiter: &#39;,&#39;,
  18. onlyAvailableTags: false
  19. });
  20. &lt;/script&gt;&lt;li class=&quot;tagit-new&quot;&gt;&lt;input type=&quot;text&quot; class=&quot;ui-widget-content ui-autocomplete-input&quot; placeholder=&quot;标签&quot; autocomplete=&quot;off&quot; role=&quot;textbox&quot; aria-autocomplete=&quot;list&quot; aria-haspopup=&quot;true&quot;&gt;&lt;/li&gt;
  21. &lt;/ul&gt;
  22. &lt;/div&gt;
英文:

In my use case I am using tag tag-it to get tags from user. I am getting the tags input in html &lt;ul&gt; form. I am using golang in server side.

html:

  1. &lt;form class=&quot;comment-form&quot; action=&quot;/add/&quot; method=&quot;POST&quot; enctype=&quot;multipart/form-data&quot;&gt;
  2. &lt;div class=&quot;form-input&quot;&gt;
  3. &lt;label for=&quot;tags_label&quot;&gt;Tags&lt;/label&gt;
  4. &lt;ul id=&quot;tags&quot;&gt;
  5. &lt;script type=&quot;text/javascript&quot;&gt;
  6. $(&quot;#myTags&quot;).tagit();
  7. var tagsArray = [&quot;C&quot;, &quot;C++&quot;, &quot;Go&quot;, &quot;Ruby&quot;];
  8. $(&quot;#tags&quot;).tagit({
  9. itemName: &quot;teamId&quot;,
  10. fieldName: &quot;teamName&quot;,
  11. availableTags: tagsArray,
  12. allowSpaces:true,
  13. caseSensitive:false,
  14. removeConfirmation:true,
  15. placeholderText:&quot;Tags&quot;,
  16. tagLimit: 5,
  17. allowDuplicates: false,
  18. singleFieldDelimiter: &#39;,&#39;,
  19. onlyAvailableTags: false
  20. });
  21. &lt;/script&gt;
  22. &lt;/ul&gt;
  23. &lt;/div&gt;
  24. &lt;/form&gt;

and in server end I am trying to get the value like below similar to other fields in the form,

  1. tags := r.FormValue(&quot;tags&quot;)
  2. log.Printf(&quot;Tags : &quot;, tags)

But it is not working. Could someone help me with this?

EDIT:
When I inspect the element this is what I see,

  1. &lt;div class=&quot;form-input&quot;&gt;
  2. &lt;label for=&quot;tags_label&quot;&gt;Tags&lt;/label&gt;
  3. &lt;ul id=&quot;tags&quot; class=&quot;tagit ui-widget ui-widget-content ui-corner-all&quot;&gt;
  4. &lt;script type=&quot;text/javascript&quot;&gt;
  5. $(&quot;#myTags&quot;).tagit();
  6. var tagsArray = [&quot;C&quot;, &quot;C++&quot;, &quot;Go&quot;, &quot;Ruby&quot;];
  7. $(&quot;#tags&quot;).tagit({
  8. itemName: &quot;teamId&quot;,
  9. fieldName: &quot;teamName&quot;,
  10. availableTags: tagsArray,
  11. allowSpaces:true,
  12. caseSensitive:false,
  13. removeConfirmation:true,
  14. placeholderText:&quot;Tags&quot;,
  15. tagLimit: 5,
  16. allowDuplicates: false,
  17. singleFieldDelimiter: &#39;,&#39;,
  18. onlyAvailableTags: false
  19. });
  20. &lt;/script&gt;&lt;li class=&quot;tagit-new&quot;&gt;&lt;input type=&quot;text&quot; class=&quot;ui-widget-content ui-autocomplete-input&quot; placeholder=&quot;Tags&quot; autocomplete=&quot;off&quot; role=&quot;textbox&quot; aria-autocomplete=&quot;list&quot; aria-haspopup=&quot;true&quot;&gt;&lt;/li&gt;
  21. &lt;/ul&gt;
  22. &lt;/div&gt;

答案1

得分: 4

找到问题了:你期望一个单一的字段,但是在tag-it选项中没有指定它。请按照以下方式使用(为了清晰起见,添加了一些注释):

  1. <script type="text/javascript">
  2. $("#myTags").tagit();
  3. var tagsArray = ["C", "C++", "Go", "Ruby"];
  4. $("#tags").tagit({
  5. fieldName: "teamName", // 隐藏输入字段的名称
  6. availableTags: tagsArray,
  7. allowSpaces:true,
  8. caseSensitive:false,
  9. removeConfirmation:true,
  10. placeholderText:"Tags",
  11. tagLimit: 5,
  12. allowDuplicates: false,
  13. singleField: true, // 使用带有fieldName名称的隐藏输入元素
  14. singleFieldDelimiter: ',', // 可选,默认值相同。
  15. onlyAvailableTags: false
  16. });
  17. </script>

在运行时(并输入标签)将使用一个隐藏的<input>,其中包含您在tag-it选项中指定的标签。

  1. <input type="hidden" style="display:none;" value="Go,another value,C" name="teamName">

在Go中,按照以下方式处理它(你在Printf中漏掉了%s):

  1. tags := r.FormValue("teamName")
  2. log.Printf("Tags: %s", tags)

然后,您可以使用strings.Split拆分标签。

英文:

Found the problem: you expected a single field but you didn't specify it in the options of tag-it. Use it as following (added some comments for clarity):

  1. &lt;script type=&quot;text/javascript&quot;&gt;
  2. $(&quot;#myTags&quot;).tagit();
  3. var tagsArray = [&quot;C&quot;, &quot;C++&quot;, &quot;Go&quot;, &quot;Ruby&quot;];
  4. $(&quot;#tags&quot;).tagit({
  5. fieldName: &quot;teamName&quot;, // The name of the hidden input field
  6. availableTags: tagsArray,
  7. allowSpaces:true,
  8. caseSensitive:false,
  9. removeConfirmation:true,
  10. placeholderText:&quot;Tags&quot;,
  11. tagLimit: 5,
  12. allowDuplicates: false,
  13. singleField: true, // Use a hidden input element with the fieldName name
  14. singleFieldDelimiter: &#39;,&#39;, // Optional, default value is same.
  15. onlyAvailableTags: false
  16. });
  17. &lt;/script&gt;

During runtime (and entering tags) a hidden &lt;input&gt; will be used, with the tag that you specified in the tag-it options.

  1. &lt;input type=&quot;hidden&quot; style=&quot;display:none;&quot; value=&quot;Go,another value,C&quot; name=&quot;teamName&quot;&gt;

In Go, handle it as following (you missed the %s in Printf):

  1. tags := r.FormValue(&quot;teamName&quot;)
  2. log.Printf(&quot;Tags: %s&quot;, tags)

You can then split the tags with strings.Split.

huangapple
  • 本文由 发表于 2016年3月28日 06:19:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/36253370.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定