Javascript:根据文本调整输入字段宽度

标签 javascript html css

我正在尝试根据此解决方案实现自动调整大小的文本字段 question

<input style="width:20px;" onkeypress="this.style.width = ((this.value.length + 1) * 5) + 'px';"  ng-model="questionButtons[$index].title" type="text" class="edt_spn active" >

它工作正常但是当用户删除字符时宽度没有调整

<input style="width:20px;" onkeypress="this.style.width = ((this.value.length + 1) * 5) + 'px';"  ng-model="questionButtons[$index].title" type="text" class="edt_spn active" >

最佳答案

尝试 onkeyup 事件。

<input style="width:20px;" onkeyup="this.style.width = ((this.value.length + 1) * 5) + 'px';console.log(this.value.length);"  ng-model="questionButtons[$index].title" type="text" class="edt_spn active" >

关于Javascript:根据文本调整输入字段宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48898996/

相关文章:

html - 为什么我的 CSS 样式在上传到 GitHub 页面后没有应用于我的 HTML?

javascript - 将动态生成的输入委托(delegate)给预输入

javascript - 如何查询溢出隐藏区域中的元素:scroll

html - Bootstrap 4 : Displaying 3 columns in two rows on narrow screens

javascript - 不刷新页面的表单提交

html - 适合对象 : contain; Calculate even height based on width percentage %?

javascript - 插入区间可观察值

html - 为什么 box-sizing 不适用于单选按钮?

javascript - 在目录中显示完整图片

javascript - 返回构造函数不产生构造函数