在我的用例中,我使用标签 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/