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.

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

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀