javascript - 使用输入创建标签
问题描述
我有一个用于编写标签的输入。我的问题是我想在输入中写入标签的任何名称,然后按回车键或单击按钮,在下面的框中创建标签,如图所示。
是)我有的
当我按下回车键或按钮加号时我想要什么
最终的
.TagInput {
width: 400px;
height: 32px;
background: #FAFAFB 0% 0% no-repeat padding-box;
border-radius: 16px;
background: url(https://cdn0.iconfinder.com/data/icons/social- messaging-ui-color-shapes/128/add-circle-blue-512.png) no-repeat scroll 370px 3px;
}
.card {
width: 435px;
height: 400px;
background: #FFFFFF 0% 0% no-repeat padding-box;
box-shadow: 0px 3px 20px #BCBCCB47;
border-radius: 8px;
}
<div class="card">
<div class="card-header header">
<h1> Tags </h1>
</div>
<div class="card-body">
<div class="form-group">
<input type="text" class="form-control TagInput" placeholder="Tag your product...">
</div>
</div>
</div>
解决方案
老实说,我对引导程序不太熟悉。但也许你可以尝试一个 Angular 解决方案。这可能是一个起点:
CSS
.your-class{
list-style-type: none;
display:inline-block;
position:relative;
background:#eee;
padding:5px;
min-width:50px;
text-align: center;
border-radius:30px;
margin-right:10px;
}
HTML
<ul>
<li *ngFor="let tag of tags" class="your-class">
{{tag}} <span class="delete-entry" (click)="deleteTagFromProduct(tag)">x</span>
</li>
</ul>
<input (change)="addTagToProduct($event)" class="form-control TagInput"/>
打字稿
private tags:string[] = [];
private addTagToProduct( $event ):void {
this.tags.push($event.target.value);
$event.target.value = "";
}
private deleteTagFromProduct( tag:string ):void {
this.tags = this.tags.filter( (a) => a!==tag );
}
推荐阅读
- python - 运行解释器时导入某些模块时出错
- javascript - Javascript 重定向到 URL 控制器
- prolog - 在 SWI Prolog 中用常见的自由变量指示列表
- xcode - 如何在 Xcode 中手动强制索引?
- kotlin - Kotlin 中是否存在诸如 Duration 但用于数字信息度量(即千字节、兆字节等)的类?
- python - 当分配给“row, col”时,“key”参数将保持什么,如下面的代码所示?
- node.js - Knex: Timeout acquiring a connection when running `knex migrate`
- python - I want to remove all strings from a list of integers. Why doesn't this work?
- python - Python/Bokeh - DataTable 触发行选择功能
- c# - Is it possible to execute code after the ServiceProvider is created?