입력 요소가 있고 사용자가 입력하면이 정적 텍스트가 항상 사용자 입력 후에 나타납니다.
h "staticTextHere"
hello "staticTextHere"
hello world "staticTextHere"
몇 가지 기술을 시도했지만 여기에 너무 많은 문제가 있습니다. 이것을 달성하는 방법에 대한 아이디어가 있습니까?
여기 내 코드가 있습니다.
<input type="text" id="staticTextId" name="staticTextName" ..... />
그리고 js;
.on('keydown', function (event) {
var textLength = document.getElementById($id).value.length;
document.getElementById($id).value = document.getElementById($id).value.substr(0,textLength - staticText.length) + staticText;
});
일반 JS 버전 : 온라인 데모
jQuery : 온라인 데모
document.getElementById("staticTextId").addEventListener('keyup', function(){
var str = this.value;
var suffix = " static text";
if(str.search(suffix) === -1){
str += suffix;
}
var actualLength = str.length - suffix.length;
// set the value
this.value = str.substr(0,actualLength) + suffix;
// set cursor position
this.setSelectionRange(actualLength,actualLength);
});
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다