我从以下资源下载了 bootstrap-tagsinput 库的源 JS 和 CSS: http://timschlechter.github.io/bootstrap-tagsinput/examples/
在我的 asp.net 网站中,我按以下方式使用它:
<asp:TextBox ID="txtCompany" style="font-size:x-large" runat="server" data-role="tagsinput" CssClass="form-control" placeholder="Add Company" />
<script type="text/javascript">
$('input #txtCompany').tagsinput();
alert($('input #txtCompany').val());
</script>
但警报实际上应该返回我创建的标签,但它返回:undefined
JS是用Master Page
编写的。 母版页
中也提到了所有 CSS 和 JS 引用。
这里有什么问题吗?
编辑:
编辑:
通过以下 jQuery 代码,我能够检索 div 的内容。
$('#btn').click(function () {
var div = document.getElementById("div");
var spans = div.getElementsByTagName("span");
for (i = 0; i < spans.length; i++) {
alert(spans[i].innerHTML);
}
});
但问题是它返回以下内容:
还显示另一个空警报!我不知道为什么!
最佳答案
你有2个选择 第一个是用浏览器上解析的id替换id
<asp:TextBox ID="txtCompany" style="font-size:x-large" runat="server" data-role="tagsinput" CssClass="form-control" placeholder="Add Company" />
<script type="text/javascript">
$('input #ContentPlaceHolder1_txtCompany').tagsinput();
alert($('input #ContentPlaceHolder1_txtCompany').val());
</script>
或者您将向文本框添加一个类作为第二个解决方案,并用类名而不是 id 替换 jquery 代码中的 id
<asp:TextBox ID="txtCompany" style="font-size:x-large" runat="server" data-role="tagsinput" class="form-control txtcomp" placeholder="Add Company" />
<script type="text/javascript">
$('input .txtcomp').tagsinput();
alert($('input .txtcomp').val());
</script>
尝试一下,我认为它会起作用
编辑 您可以使用此功能在警报中显示添加的项目值
$('input').on('itemAdded', function (event) {
alert(event.item)
});
关于javascript - Bootstrap标签输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32049569/