简体   繁体   English

如何将标签添加到Asp.net文本框中?

[英]How to add Tag into Asp.net Textbox?

I am trying to develop an web application in asp.net where my goal is to create a textbox where user can type multiple phone numbers with comma and then it shows as tags. 我正在尝试在asp.net中开发一个Web应用程序,我的目标是创建一个文本框,用户可以用逗号键入多个电话号码,然后将其显示为标签。 I want is as below: 我想要的如下:

在此处输入图片说明

I can already achieve this with the following code: 我已经可以使用以下代码实现此目的:

        <div class="control-group">
            <label class="control-label col-md-3 col-sm-3 col-xs-12">Input Tags</label>
            <div class="col-md-9 col-sm-9 col-xs-12">
                <input id="tags_1" type="text" class="tags form-control" value="social, adverts, sales" />
                <div id="suggestions-container" style="position: relative; float: left; width: 250px; margin: 10px;"></div>
            </div>
        </div>

        <script>
    function onAddTag(tag) {
        alert("Added a tag: " + tag);
    }

    function onRemoveTag(tag) {
        alert("Removed a tag: " + tag);
    }

    function onChangeTag(input, tag) {
        alert("Changed a tag: " + tag);
    }

    $(function () {
        $('#tags_1').tagsInput({
            width: 'auto'
        });
    });

but the problem is when I am making it as 但是问题是当我把它做成

  runat="server"

Then it is not showing with the tags anymore in the front end. 然后,它不再在前端显示标签。

So, please suggest me how shall I do this. 所以,请建议我该怎么做。

Setting an input to runat=server prompts asp.Net to generate a totally unique id. 将输入设置为runat=server会提示asp.Net生成完全唯一的ID。 If you inspect the HMTL you'll see the id is not tags_1 . 如果您检查HMTL,您会看到ID不是tags_1 You can turn this off by setting the ClientIDMode to static in your text box 您可以通过在文本框中将ClientIDMode设置为static来关闭此功能

<asp:Textbox runat="server" ClientIDMode="static".../>

As you can see you'll have to use a asp.Net textbox also, not just set a standard html one to runat="server" 如您所见,您还必须使用asp.Net文本框,而不仅仅是将标准html文本框设置为runat="server"

your other option is to change your jQuery to select by something other than id, such as class: 您的另一种选择是将jQuery更改为通过id以外的其他内容进行选择,例如class:

<input id="tags_1" type="text" class="tags form-control 
     tags_1" value="social, adverts, sales" />

$('.tags_1').tagsInput({..});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM