2010-12-16 2 views
1

여기 내 코드입니다 ..하지만이 코드를 이해할 수 없습니다. PLZ 그래서 기본적으로이 코드는 텍스트 영역은 130 개 문자의 문자 제한을하게하고 사용자가 입력 할 수있어 얼마나 더 많은 방법을 보여줍니다 ... 저다음 코드를 설명 할 수 있습니다

$('.maxlength') 

    .after("<span></span>") 

    .next() 

    .hide() 

    .end() 

    .keypress(function(e) { 

     var current = $(this).val().length; 

     if (current >= 130) { 

      if (e.which != 0 && e.which != 8) { 

       e.preventDefault(); 

      } 

     } 

     $(this).next().show().text(130 - current); 

    }); 
+0

그건 그렇고, 이것을 달성하는 끔찍한 방법입니다. 이 코드를 사용하지 마십시오! –

+1

@paxdiablo : 내 코드는 mee가 작성한 것이 아닙니다 .. 이걸 어떻게 알면 .. 도움을 요청할 수 있습니다 .. 가능하면 도움 말을하지 마십시오 .. 죄송합니다. 감사합니다. – Mihir

+0

나는 많은 시간을 우리가 작업하고 있음에 동의합니다. 다른 사람들이 이미 개발 한 코드 ... – kobe

답변

6
$('.maxlength') // select all items with class 'maxlength' 

.after("<span></span>") // insert a span after 

.next() // move to the span 

.hide() // hide the span 

.end() // go back to originally selected element 

.keypress(function(e) { // add a keypress event handler function 

    var current = $(this).val().length; // get the length of the input value (a string) 

    if (current >= 130) { //if it's long 

     if (e.which != 0 && e.which != 8) { // and if certain keys weren't pressed (delete?) 

      e.preventDefault(); // don't do what those keys would normally do - i.e. ignore the keypress 

     } 

    } 

    $(this).next().show().text(130 - current); // show the remaining chars in the newly added span 

}); 

에게 도움이됩니다.

+0

동생에게 너무 감사드립니다. 그래서 여기에 end()는 현재 요소를 중지하고 이전의 오른쪽으로 이동한다는 의미입니다! 우리가 end()를 사용하면 이전의 것 또는 원래의 것으로 이동합니까? – Mihir

+0

@Mihir : http://api.jquery.com/end/ : * 현재 체인에서 가장 최근의 필터링 작업을 끝내고 일치하는 요소 집합을 이전 상태로 되돌립니다. * –

+0

고맙습니다. – Mihir

관련 문제