如何在文本区域中使用不可删除的单词(部分文字) [英] how to have undeletable words(part of text) in textarea
问题描述
我正在开发一个应用,该应用需要无法删除的textarea中的一部分文本,而他们可以将其文本添加到textarea
中.
I am developing an application which needs a part of text in the textarea undeletable while they can add their text to the textarea
.
例如,当用户访问网页时,它具有一个文本区域,该文本区域具有该用户的文本{username}
.用户可以添加更多文本,例如{username} hi the the earth is globe
,但应该无法从该文本区域中删除{username}
.
For example when user come to a web page it has a textarea which has a text {username}
of that user. The user can add more text like {username} hi the the earth is globe
but should be unable to delete {username}
from that textarea.
我正在将Jquery用作javascript类,是否知道我们可以在Jquery中实现它?
I am using Jquery as the javascript class, any idea that we can achieve it in Jquery?
推荐答案
一点也不知道,但是我明白了,它仍然需要很多改进
Not sure at all but I got this and it still need to be improved a lot
<script>
$(function (){
$('#txtarea').keyup(function (){check()});
})
var txt = "username";
//txt is reg exp
function check(){
var txtarea = $('#txtarea');
var val = $(txtarea).val();
if(val.match('^'+txt)){
//do nothing
}
else
{
$(txtarea).val(txt+" "+val);
}
}
</script>
<textarea id="txtarea">username</textarea>
<a href="#" onclick="check()">check</a>
[UPDATE]
<script>
$(function (){
$('#txtarea').keyup(function (){check()});
})
var txt = "username";
//txt is reg exp
function check(){
var txtarea = $('#txtarea');
var val = $(txtarea).val();
if(val.match('^'+txt)){
//do nothing
}
else
{
//this is quite a bit flawed
//and i m terrible with regex
//do try to become creative
val = val.replace('usernam', '');
val = val.replace('userna', '');
val = val.replace('usern', '');
val = val.replace('user', '');
val = val.replace('use', '');
val = val.replace('us', '');
val = val.replace('u', '');
$(txtarea).val(txt+" "+val);
}
}
</script>
<textarea id="txtarea">username</textarea>
<a href="#" onclick="check()">check</a>
这篇关于如何在文本区域中使用不可删除的单词(部分文字)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!