简体   繁体   中英

How to prepend some text in a text input and make it undeletable while typing remaining text?

如何在文本输入中添加一些前置文本,如果人们在填写表格的文本字段时输入其余文本,则不会删除已经添加的前置文本?

Here is a way to prepend a text to an input and make it unremovable.

const input = document.getElementById('my-input')
const prependedText = "This is a prepended text"
const prependedTextLength = prependedText.length

textarea.value = prependedText

textarea.onkeyup = (ev) => {
    if (textarea.value.length < prependedTextLength) {
        textarea.value = prependedText
    }
}

The script prepends the text to the input and watches for changes to that input. If it detects that the length of the text is smaller than the prepended one, it will reset to that text.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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