문제

function count(){  
  var val   = $.trim($('textarea').val()),  
      words = val.replace(/\s+/gi, ' ').split(' ').length,
      chars = val.length;
  if(!chars)words=0;

 return words;
}

This function always return 0? Please help.

도움이 되었습니까?

해결책

You can split the textarea value on spaces and then get the words length in it. Try this:

var words = $('textarea').val().split(' ');
alert(words.length);//or return words.length;

working Demo

다른 팁

text_area = $('textarea').val();
text_area.length == 0 ? 0 : text_area.trim().split(/\s+\b/).length
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top