简体   繁体   English

Javascript - 在开始文本输入时只读

[英]Javascript - Readonly on beginning text input

I have this input with some text (it could be personne or contacts ):我有一些文本输入(可能是personnecontacts ):

Is it possible to block modification of the first word?是否可以阻止修改第一个单词?

If there is personne or contacts in the input text, prevent the deletion of these from the input field but only allow entry after these words.如果输入文本中有personnecontacts ,请防止从输入字段中删除这些,但只允许在这些单词之后输入。

Here's simple JS solution, you can write in it but can not delete:这是一个简单的JS解决方案,你可以在里面写但不能删除:

 document.querySelector('#myInput').addEventListener('keyup', val, false); function val() { if (this.value.includes("personne ") === false) { this.value = "personne "; } }
 <input type="text" value="personne " size="30" id="myInput">

And you can even make sure if user types something, and selects the personne with mouse and delete it with keyboard, to append it back again with value typed after it:您甚至可以确定用户是否输入了某些内容,然后用鼠标选择personne并用键盘将其删除,然后再返回 append 并在其后键入值:

 document.querySelector('#myInput').addEventListener('keyup', val, false); function val() { if (this.value.includes("personne ") === false) { this.value = "personne " + this.value.substr(this.value.indexOf(' ') + 1); this.value = this.value.replace("personne personne", "personne "); } }
 <input type="text" value="personne " size="30" id="myInput">

you can do something like this:你可以这样做:

 input{ padding-left:70px; }.input-container{ position: relative; }.input-container:after{ content: 'prersonne'; position:absolute; top: 50%; left: 5px; transform: translateY(-50%); }
 <div class="input-container"> <input type="text"> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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