Programing

IE8과 JQuery의 trim ()

lottogame 2020. 8. 14. 08:11
반응형

IE8과 JQuery의 trim ()


나는 다음과 같이 trim ()을 사용하고 있습니다.

if($('#group_field').val().trim()!=''){

group_field텍스트 유형의 입력 요소는 어디에 있습니까 ? 이것은 Firefox에서 작동하지만 IE8에서 시도하면 다음 오류가 발생합니다.

Message: Object doesn't support this property or method

trim ()을 제거하면 IE8에서 잘 작동합니다. trim () 사용하는 방식이 맞다고 생각 했습니까?

도움을 주셔서 감사합니다.


대신 이것을 시도하십시오.

if($.trim($('#group_field').val()) != ''){

더 많은 정보:


다음과 같이을 사용해야합니다 $.trim.

if($.trim($('#group_field').val()) !='') {
    // ...
}

내가 아는 한 Javascript String에는 트림 메소드가 없습니다. 기능 트림을 사용하려면

<script>
    $.trim(string);
</script>

또 다른 옵션은 String누락 된 경우 직접 메서드를 정의하는 것입니다 .

if(typeof String.prototype.trim !== 'function') {
  String.prototype.trim = function() {
    //Your implementation here. Might be worth looking at perf comparison at
    //http://blog.stevenlevithan.com/archives/faster-trim-javascript
    //
    //The most common one is perhaps this:
    return this.replace(/^\s+|\s+$/g, ''); 
  }
}

그런 다음 trim브라우저에 관계없이 작동합니다.

var result = "   trim me  ".trim();

jQuery를 사용하여 텍스트 유형으로 입력을 전역 적으로 트림하려면 :

/**
 * Trim the site input[type=text] fields globally by removing any whitespace from the
 * beginning and end of a string on input .blur()
 */
$('input[type=text]').blur(function(){
    $(this).val($.trim($(this).val()));
});

참고 URL : https://stackoverflow.com/questions/3439316/ie8-and-jquerys-trim

반응형