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

标签 html forms go html-lists

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

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>

在服务器端,我试图获取类似于表单中其他字段的值,

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

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

编辑: 当我检查元素时,这就是我所看到的,

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

最佳答案

发现问题:您需要一个字段,但您没有在 options 中指定它的 tag-it .按如下方式使用它(为清楚起见添加了一些注释):

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

在运行时(和输入标签)一个隐藏的 <input>将与您在 tag-it 中指定的标签一起使用选项。

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

在 Go 中,按如下方式处理(您错过了 %s 中的 Printf):

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

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

关于html - 如何在golang中获取<ul>的表单值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36253370/

相关文章:

javascript - 通过 jQuery 仅选择一个对象并使用表单提交

单击 "Submit"后重定向用户/更改文本的 Javascript 函数

jquery - 如何使用 Jquery datepicker 禁用基于表单字段和全局变量的一周中的某一天?

go - 在 golang 中使用 gmail API 发送带附件的电子邮件

go - beego模板值范围执行 "content"不是结构体类型的字段

Javascript JQuery 追加不起作用

android - 如何在 Android 和黑莓模拟器中打开本地 .html 文件进行测试?

html - Internet Explorer 中的 Firebug 替代品

输入字段的 Javascript 设置值

go - 使用多写入器计算加密数据的哈希值