[英]Getting value of multiple <span> tags in HTML/JavaScript?
I'm hoping someone can help me out with this...I followed the tutorial here: http://jsbin.com/hehurot/5/edit?html,css,js,console,output to create a field on an HTML form which contains tags. 我希望有人可以帮我解决这个问题...我在这里遵循了该教程: http : //jsbin.com/hehurot/5/edit? html,css, js , console,输出以在HTML上创建字段包含标签的表格。 Now when I click submit on my form I want to grab the data in that field with tags and do something with it (save to database).
现在,当我单击表单上的“提交”时,我想使用标签获取该字段中的数据并对其进行处理(保存到数据库)。
How would I go about grabbing the data from the field since each tag is a ? 因为每个标签都是一个标记,我将如何从字段中获取数据?
Here is my current code for the tag field: 这是我当前用于标签字段的代码:
<div class="form-group">
<label class="col-sm-3 control-label">Equipment</label>
<div id="tags" class="col-sm-9">
<span>Hardhat</span>
<span>Steel Toe Boots</span>
<input id="equipment" type="text" name="equipment" value="" placeholder="Add equipment" >
</div>
</div>
The JS to make the tag field work (the CSS is available on the link above) 使标记字段起作用的JS(上面的链接提供了CSS)
<script type="text/javascript">
$(function(){
$("#tags input").on({
focusout : function() {
var txt= this.value.replace(/[^a-z0-9\+\-\.\#]/ig,'');
if(txt) $("<span/>",{text:txt.toLowerCase(), insertBefore:this});
this.value="";
},
keyup : function(ev) {
if(/(188|13)/.test(ev.which)) $(this).focusout();
}
});
$('#tags').on('click', 'span', function() {
/*if(confirm("Remove "+ $(this).text() +"?")) */$(this).remove();
});
});
</script>
Now in my JS file, how can I get the values saved as separate 's? 现在在我的JS文件中,如何获取另存为的值?
I tried the following but this does not work, returns empty string: 我尝试了以下操作,但这不起作用,返回空字符串:
'equipment': $('#tags').val()
Thanks in advance for the help! 先谢谢您的帮助!
Try this: 尝试这个:
var tags = $('#tags span').map(function () {
return $(this).text();
});
Follow-up: 跟进:
I modified it a bit to save all the values from the to a comma separated list.
我对其进行了一些修改,以将的所有值保存到逗号分隔的列表中。
var tags = $('#tags span').map(function () {
return $(this).text();
});
tags = Array.prototype.join.call(tags, ",");
console.log(tags);
Followup question, in Javascript how can I add a tag programatically?
跟进问题,在Javascript中如何以编程方式添加标签? I want to loop through the comma separated list and add each value back to tags.
我想遍历逗号分隔的列表,并将每个值添加回标记中。
$('#tags').append(tags.split(",").map(function (tag) {
return "<span>" + tag + "</span>";
}));
Using jQuery's .each()
http://api.jquery.com/each/ : 使用jQuery的
.each()
http://api.jquery.com/each/ :
a = []; $('#tags > span').each(function(){a.push(this.innerHTML)});
// a => ["php", "c++", "jquery"]
how can I add a
<span>
tag programmatically?如何以编程方式添加
<span>
标签? I want to loop through the comma separated list and add each value back to<span>
tags.我想遍历逗号分隔的列表,并将每个值添加回
<span>
标记。
Using jQuery.each()
http://api.jquery.com/jQuery.each/ : 使用
jQuery.each()
http://api.jquery.com/jQuery.each/ :
$.each(["php", "c++", "jquery"], function(i,v){
$('#tags').prepend($(document.createElement('span')).text(v))
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.