简体   繁体   English

单击后将初始值保留在文本框中

[英]Keep initial value in textbox after click

I need to keep an initial value/placeholder in my textbox after it's clicked. 单击它后,我需要在文本框中保留一个初始值/占位符。 It needs to be readonly but allow the user to add characters in after the preset text: 它必须是只读的,但允许用户在预设文本之后添加字符:

<input type="text" placeholder="I will " name="title" required maxlength="50">

When the user clicks in the textbox, I need it to move their cursor to after the word "I will" and allow them to type. 当用户单击文本框时,我需要它将光标移动到“我会”一词之后并允许他们键入。 They cannot be able to delete the words "I will". 他们无法删除“我会”一词。

Check this basic example based on @Josep suggestion : 根据@Josep建议检查以下基本示例:

 label{ border: 1px solid; } input{ border: 0px; height: 100%; font-family: inherit; outline: none; font-size: 16px; padding: 0px; } 
 <label>I will <input type="text" name="title" required maxlenght="50" /></label> 

Outline: none will remove the blue box around the input in major browsers. Outline: none在主要浏览器中, Outline: none会删除输入周围的蓝色框。 (Just one aesthetic improvement) (仅一项美学改进)

Try using focusout event to reset value , readonly attributes after click event 尝试在click事件后使用focusout事件重置valuereadonly属性

 $("input").on({ click:function(e) { $(this).removeAttr("readOnly") }, focusout: function(e) { this.value = this.getAttribute("placeholder") + this.value.replace(/^I will /, ""); $(this).prop("readOnly", true); } }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> </script> <input type="text" placeholder="I will " name="title" required maxlength="50" readonly> 

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

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