2016-11-17 6 views
2
//loop thought all the sku field 
$('.optionSku').each(function() { 

    //if found field are empty 
    if (jQuery.trim(this.value) == "") { 

     //loop thought all the name field to copy to the empty field 
     $('.optionName').each(function() { 

      if ($(this).closest("tr").find(".optionSku").val() == "") { 

       empty++; 
       $(this).closest("tr").find(".optionSku").val($(this).val()); 
      } 

     }); 
    } 
}) 

JQuery를 사용하여 this.value에서 공백을 제거하는 방법은 무엇입니까? if (jQuery.trim(this.value) == "")을 넣으려고 시도하지만 인라인 공간을 제거 할 수 없으며 앞뒤 공백 만 제거합니다..value에서 공백을 제거하는 자바 스크립트

+0

를 사용 인라인하려면 공간

//loop thought all the sku field $('.optionSku').each(function() { //if found field are empty if ($.trim(this.value)=="") { //loop thought all the name field to copy to the empty field $('.optionName').each(function() { if ($(this).closest("tr").find(".optionSku").val() == "") { empty++; $(this).closest("tr").find(".optionSku").val($(this).val()); } }); } }); 

를 사용 후행 제거하려면 찾으십시오. (". optionSku") .val ($ (this) .val(). trim()); $ (trim) $ (this) .closest ("tr"). find (". optionSku"). –

+0

안녕하세요. 단어 사이에있는 모든 사람들을 제거하고 싶습니까? –

답변

2
this.value = this.value.trim(); 
//you need to assign the trimmed value back to it. 

trim 것을 IE에서 < 9 버전 availbale하지 않을 수 있습니다. 그래서 당신은 사용할 수 있습니다

this.value = $.trim(this.value); 
+0

.trim은 $ .trim과 호환되지 않습니다 – mplungjan

+0

이에 동의 할 수 없습니다 :)는 이에 따라 변경됩니다. –

0

이 시도 :

if(this.value.trim()==""){ 
    //code here 
} 
+0

.trim은 $ .trim과 호환되지 않습니다. – mplungjan

0

.replace (//g이, '') 은 g 문자는 전체 문자열을 통해 검색을 반복하는 것을 의미합니다. 당신은 모든 공백, 그리고뿐만 아니라 문자 공백 문자와 일치 \뿐만 아니라이야 사용하려면 :

.replace (/ \ S/g, '')

1

이 자바 스크립트에서 어떻게 트림 작품이다 .

var val = this.value.trim(); 
+0

이미 답변되었으며 jQuery를 사용하므로 호환 가능한 $ .trim이 더 많이 사용됩니다. – mplungjan

2

$.trim() 함수는 제공된 문자열의 시작과 끝에서 모두 (비 침입 공간 포함) 바꿈, 공백 및 탭을 제거한다. 이 공백 문자가 문자열 중간에 있으면 해당 문자가 보존됩니다.
당신은 앞과 $ (이) .closest ("TR")) 당신이 공간은 당신이 일을 사용할 수

//loop thought all the sku field 
$('.optionSku').each(function() { 
    //if found field are empty 
    if (this.value.trim()=="") { 
     //loop thought all the name field to copy to the empty field 
     $('.optionName').each(function() { 
      if ($(this).closest("tr").find(".optionSku").val() == "") { 
      empty++; 
      $(this).closest("tr").find(".optionSku").val($(this).val()); 
      } 
     }); 
    } 
});