2012-12-23 17 views
5

Ho cercato di aggiungere un conteggio di parole e caratteri a un'area di testo utilizzando jQuery, anche se tutto ciò che ho trovato sono plug-in per limitare caratteri e parole.Conteggio parole e caratteri utilizzando jQuery

Desidero aggiornare in tempo reale, in modo che ogni volta che un utente aggiunge un carattere o una parola, il contatore si aggiorni.

C'è un modo semplice per verificare parole e caratteri?

+0

Utilizzare .lunghezza per controllare il conteggio dei caratteri. Il conteggio delle parole di solito non ha importanza, a meno che non sia qualcosa di simile al saggio, dal momento che puoi creare un verrryyyyyyyyyyyyyyyyloooooooooooooooooooooooongwooooooooooooooord. – nhahtdh

+1

$ ("textarea"). Html(). Split ("") .length –

+4

Odio sembrare maleducato, ma ho cercato su google "[parole di conteggio jquery] (https://www.google.co.uk/#hl= it & safe = off tbo = d & sclient = psy-ab & q = jquery + count + parole & oq = jquery + count + parole & gs_l = hp.3..0l4.973.5328.0.5436.18.8.0.9.9.0.200.715.7j0j1.8.0.les% 3B .. 0.0 ... 1c.1.L1JWGBTTwDs & pbx = 1 & bav = on.2, or.r_gc.r_pw.r_cp.r_qf. & Bvm = bv.1355534169, d.d2k & fp = f42fee5d8e10998d & bpcl = 40096503 & biw = 1235 & bih = 683) "e il [primo risultato è esattamente quello che stai cercando] (http://stackoverflow.com/questions/7422192/jquery-count-words-in-real-time). E per la lunghezza, è solo lunga. Quindi davvero non hai cercato molto duramente. –

risposta

45

function wordCount(val){ 
 
    var wom = val.match(/\S+/g); 
 
    return { 
 
     charactersNoSpaces : val.replace(/\s+/g, '').length, 
 
     characters   : val.length, 
 
     words    : wom ? wom.length : 0, 
 
     lines    : val.split(/\r*\n/).length 
 
    }; 
 
} 
 

 

 
var textarea = document.getElementById("text"); 
 
var result = document.getElementById("result"); 
 

 
textarea.addEventListener("input", function(){ 
 
    var v = wordCount(this.value); 
 
    result.innerHTML = (
 
     "<br>Characters (no spaces): "+ v.charactersNoSpaces + 
 
     "<br>Characters (and spaces): "+ v.characters + 
 
     "<br>Words: "+ v.words + 
 
     "<br>Lines: "+ v.lines 
 
); 
 
}, false);
<textarea id="text"></textarea> 
 
<div id="result"></div>

jsFiddle demo

+0

C'è un modo per aggiornarlo in tempo reale? – user1919937

+0

Certo aggiunta una demo nella mia risposta –

+0

Funziona! Grazie! Inoltre, ho provato a [aggiungere questo] (http://jsfiddle.net/deepumohanp/jZeKu/) anche se ho avuto un errore di sintassi, sai cosa lo farà? – user1919937

-1

W/O jQuery:

this.innerHTML gives you the textarea content. 
this.innerHTML.length gives you the number of characters. 

Con jQuery:

$(this).html() 

ecc

Sono sicuro che si può trovare con un semplice algoritmo di conteggio di parola.

+0

Textarea ha contenuto in HTML non in valore. –

+0

sì, corretto, tnx – TheZuck

+0

L'ho provato. Entrambi possibili .val(), .html() .... –

6
char_count = $("#myTextArea").val().length; 
word_count = $("#myTextArea").val().split(" ").length; 
+1

Questo è un approccio molto semplice, ma non tiene in considerazione lo spazio bianco finale o lo spazio multiplo in successione, che in un approccio manuale non sarebbe considerato una nuova parola. –

-1
jQuery(document).ready(function(){ 
    var count = jQuery("#textboxid").text().length; 
}); 
+0

Non voglio limitare parole e caratteri, voglio solo contarli. – user1919937

+0

length is attribute, String has not method length() –

0
var txt = $('.content')[0].text() 
    , charCount = txt.length 
    , wordCount = txt.replace(/[^\w ]/g, "").split(/\s+/).length 
    ; 
$('#somwhereInYourDocument').text("The text had " + charCount + " characters and " + wordCount +" words"); 

Read more

Problemi correlati