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

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

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

问题

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

html:

   &lt;form class=&quot;comment-form&quot; action=&quot;/add/&quot; method=&quot;POST&quot; enctype=&quot;multipart/form-data&quot;&gt;
    &lt;div class=&quot;form-input&quot;&gt;
      &lt;label for=&quot;tags_label&quot;&gt;标签&lt;/label&gt;
      &lt;ul id=&quot;tags&quot;&gt;
        &lt;script type=&quot;text/javascript&quot;&gt;
          $(&quot;#myTags&quot;).tagit();
          var tagsArray = [&quot;C&quot;, &quot;C++&quot;, &quot;Go&quot;, &quot;Ruby&quot;];
          $(&quot;#tags&quot;).tagit({
              itemName: &quot;teamId&quot;,
              fieldName: &quot;teamName&quot;,
              availableTags: tagsArray,
              allowSpaces:true,
              caseSensitive:false,
              removeConfirmation:true,
              placeholderText:&quot;标签&quot;,
              tagLimit: 5,
              allowDuplicates: false,
              singleFieldDelimiter: &#39;,&#39;,
              onlyAvailableTags: false
          });
        &lt;/script&gt;
      &lt;/ul&gt;
    &lt;/div&gt;
   &lt;/form&gt;

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

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

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

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

&lt;div class=&quot;form-input&quot;&gt;
    &lt;label for=&quot;tags_label&quot;&gt;标签&lt;/label&gt;
    &lt;ul id=&quot;tags&quot; class=&quot;tagit ui-widget ui-widget-content ui-corner-all&quot;&gt;
        &lt;script type=&quot;text/javascript&quot;&gt;
            $(&quot;#myTags&quot;).tagit();
            var tagsArray = [&quot;C&quot;, &quot;C++&quot;, &quot;Go&quot;, &quot;Ruby&quot;];
            $(&quot;#tags&quot;).tagit({
                    itemName: &quot;teamId&quot;,
                    fieldName: &quot;teamName&quot;,
                    availableTags: tagsArray,
                    allowSpaces:true,
                    caseSensitive:false,
                    removeConfirmation:true,
                    placeholderText:&quot;标签&quot;,
                    tagLimit: 5,
                    allowDuplicates: false,
                    singleFieldDelimiter: &#39;,&#39;,
                    onlyAvailableTags: false
            });
        &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;
    &lt;/ul&gt;                                            
&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:

   &lt;form class=&quot;comment-form&quot; action=&quot;/add/&quot; method=&quot;POST&quot; enctype=&quot;multipart/form-data&quot;&gt;
    &lt;div class=&quot;form-input&quot;&gt;
      &lt;label for=&quot;tags_label&quot;&gt;Tags&lt;/label&gt;
      &lt;ul id=&quot;tags&quot;&gt;
        &lt;script type=&quot;text/javascript&quot;&gt;
          $(&quot;#myTags&quot;).tagit();
          var tagsArray = [&quot;C&quot;, &quot;C++&quot;, &quot;Go&quot;, &quot;Ruby&quot;];
          $(&quot;#tags&quot;).tagit({
              itemName: &quot;teamId&quot;,
              fieldName: &quot;teamName&quot;,
              availableTags: tagsArray,
              allowSpaces:true,
              caseSensitive:false,
              removeConfirmation:true,
              placeholderText:&quot;Tags&quot;,
              tagLimit: 5,
              allowDuplicates: false,
              singleFieldDelimiter: &#39;,&#39;,
              onlyAvailableTags: false
          });
        &lt;/script&gt;
      &lt;/ul&gt;
    &lt;/div&gt;
   &lt;/form&gt;

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

tags 			:= r.FormValue(&quot;tags&quot;)
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,

&lt;div class=&quot;form-input&quot;&gt;
    &lt;label for=&quot;tags_label&quot;&gt;Tags&lt;/label&gt;
    &lt;ul id=&quot;tags&quot; class=&quot;tagit ui-widget ui-widget-content ui-corner-all&quot;&gt;
        &lt;script type=&quot;text/javascript&quot;&gt;
            $(&quot;#myTags&quot;).tagit();
            var tagsArray = [&quot;C&quot;, &quot;C++&quot;, &quot;Go&quot;, &quot;Ruby&quot;];
            $(&quot;#tags&quot;).tagit({
                    itemName: &quot;teamId&quot;,
                    fieldName: &quot;teamName&quot;,
                    availableTags: tagsArray,
                    allowSpaces:true,
                    caseSensitive:false,
                    removeConfirmation:true,
                    placeholderText:&quot;Tags&quot;,
                    tagLimit: 5,
                    allowDuplicates: false,
                    singleFieldDelimiter: &#39;,&#39;,
                    onlyAvailableTags: false
            });
        &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;
    &lt;/ul&gt;                                            
&lt;/div&gt;

答案1

得分: 4

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

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

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

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

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

tags := r.FormValue("teamName")
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):

&lt;script type=&quot;text/javascript&quot;&gt;
    $(&quot;#myTags&quot;).tagit();
    var tagsArray = [&quot;C&quot;, &quot;C++&quot;, &quot;Go&quot;, &quot;Ruby&quot;];
    $(&quot;#tags&quot;).tagit({
        fieldName: &quot;teamName&quot;, // The name of the hidden input field
        availableTags: tagsArray,
        allowSpaces:true,
        caseSensitive:false,
        removeConfirmation:true,
        placeholderText:&quot;Tags&quot;,
        tagLimit: 5,
        allowDuplicates: false,
        singleField: true, // Use a hidden input element with the fieldName name
        singleFieldDelimiter: &#39;,&#39;, // Optional, default value is same.
        onlyAvailableTags: false
    });
&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.

&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):

tags := r.FormValue(&quot;teamName&quot;)
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:

确定