programing tip

커서 위치를 잃지 않고 입력 값 업데이트

itbloger 2020. 10. 8. 07:46
반응형

커서 위치를 잃지 않고 입력 값 업데이트


중복 가능성 :
자바 스크립트 교체에서 커서가 입력 필드의 끝으로 점프하는 것을 중지

JavaScript를 사용하여 텍스트 상자의 값을 소문자로 지정하고 싶습니다. 아래 코드를 시도했지만 키를 누를 때마다 커서가 입력 끝으로 이동합니다. 이것을 어떻게 피할 수 있습니까?

$("#beLowerCase").keyup(function(){
    $(this).val( $(this).val().toLowerCase() );
});

$("#beLowerCase").on('input', function(){

    // store current positions in variables
    var start = this.selectionStart,
        end = this.selectionEnd;

    this.value = this.value.toLowerCase();

    // restore from variables...
    this.setSelectionRange(start, end);
});

( 바이올린 )


이것은 실제로 CSS에서도 작동합니다.

#beLowerCase{
  text-transform:lowercase;
}

그리고 서버는 실제 소문자를 처리 할 수 ​​있습니다.

참고 URL : https://stackoverflow.com/questions/14508707/updating-an-inputs-value-without-losing-cursor-position

반응형