英文:
How to get form value of <ul> in golang?
问题
在我的用例中,我使用标签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">标签</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:"标签",
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">标签</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:"标签",
tagLimit: 5,
allowDuplicates: false,
singleFieldDelimiter: ',',
onlyAvailableTags: false
});
</script><li class="tagit-new"><input type="text" class="ui-widget-content ui-autocomplete-input" placeholder="标签" autocomplete="off" role="textbox" aria-autocomplete="list" aria-haspopup="true"></li>
</ul>
</div>
英文:
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
得分: 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):
<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
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论