dongshi1188 2016-03-27 22:19
浏览 113
已采纳

如何在golang中获取<ul>的表单值?

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

html:

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

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

tags            := r.FormValue("tags")
log.Printf("Tags : ", tags)

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

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

<div class="form-input">
    <label for="tags_label">Tags</label>
    <ul id="tags" class="tagit ui-widget ui-widget-content ui-corner-all">
        <script type="text/javascript">
            $("#myTags").tagit();
            var tagsArray = ["C", "C++", "Go", "Ruby"];
            $("#tags").tagit({
                    itemName: "teamId",
                    fieldName: "teamName",
                    availableTags: tagsArray,
                    allowSpaces:true,
                    caseSensitive:false,
                    removeConfirmation:true,
                    placeholderText:"Tags",
                    tagLimit: 5,
                    allowDuplicates: false,
                    singleFieldDelimiter: ',',
                    onlyAvailableTags: false
            });
        </script><li class="tagit-new"><input type="text" class="ui-widget-content ui-autocomplete-input" placeholder="Tags" autocomplete="off" role="textbox" aria-autocomplete="list" aria-haspopup="true"></li>
    </ul>                                            
</div>
  • 写回答

1条回答 默认 最新

  • douduiyun6125 2016-03-28 01:39
    关注

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

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

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

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

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

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

    You can then split the tags with strings.Split.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程