Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
369 views
in Technique[技术] by (71.8m points)

javascript - 在输入其余文本时,如何在文本输入中添加一些文本并使其无法删除?(How to prepend some text in a text input and make it undeletable while typing remaining text?)

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

  ask by Oliver Afabuike translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

(希望这可以帮助。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...