我正在尝试通过复选框添加保留文本到一定限制。假设我的最大字符数为 30(带空格)。那么用户不应该超过 30。为此我写如下
(注意:这个问题已经被问过,但不清楚)
clickCheck(appendVal, checked): void {
if (checked) {
if (this.baseName.length > 30) {
this.baseName.substr(0, 30);
} else {
this.locationName = this.locationName + '' + appendVal;
this.baseName = this.locationName + ":" + this.personName;
}
}
}
<div class="displayInlineBlock wdh100p">
<input type="checkbox" value="this cannot be delivered in that location" (change)="clickCheck($event.target.getAttribute('appendVal'), $event.target.checked)">
<label>add this text</label>
</div>
复选框的值将反射(reflect)在直接 div 中
感谢您参与其中,非常感谢您的帮助
最佳答案
if (checked) {
this.locationName = this.locationName + '' + appendVal;
this.baseName = this.locationName + ":" + this.personName;
if( this.baseName.length > 30){
this.baseName = this.baseName.substr(0, 30);
}
}
将想要的内容添加到字符串中,然后 trim 它。
关于javascript - 如果值的长度达到最大长度,如何在 div 和 tirm/slice 中添加复选框的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45192275/