2013-08-19 46 views
0

我的HTML代碼是這樣的:數字符

<div class="control-group"> 
    <label class="control-label">Message Body 
    <p class="showlimit"><!-- this shows character limit for the message --> 
     <span id="charsLeft"></span>/<span id="charsLimit"></span> 
    </p> 
    </label> 
    <div class="controls"> 
    <textarea rows="7" id="msg" name="msg" class="field span5"></textarea> 
    </div> 
</div> 

和我的jQuery代碼是這樣的:

$('#charsLimit').text(1200); 
$('#msg').limit($('#charsLimit').text(),'#charsLeft'); 

我想表現留在ID charsLeft字符數出1200(即charsLimit的值) 在正常的基礎上它的工作,但在豐富的編輯水平使用阿羅哈編輯它不工作,因爲它取代文字區 div像這樣:

<!-- rest of the above code is same --> 
<div class="controls"> 
    <textarea id="msg" class="field span5" name="msg" rows="7" 
     style="display: none;"><!-- observe the style displaying none --> 
    </textarea> 
    <!-- 
    msg replaced with msg-aloha hence not able to perform normal jquery on msg 
    --> 
    <div id="msg-aloha" class="aloha-textarea aloha-editable 
     aloha-block-blocklevel-sortable ui-sortable" 
     contenteditable="true" style="height: 140px; width: 456px;"> 
     <br class="aloha-cleanme" style=""> 
    </div> 
</div> 

我無法理解該怎麼辦...

回答

1

看看Limiting Number of Characters in a ContentEditable div

嘗試

$('#charsLimit').text(1200); 
$('#msg-aloha').keyup(function(e){ $('#charsLeft').text($('#charsLimit').text() -check_charcount(e)); }); 
function check_charcount(e) 
{ 
    var char_count = $('#msg-aloha').text().length; 
    if(e.which != 8 && char_count >= $('#charsLimit').text()) 
    { 
     e.preventDefault(); 
    } 
    return char_count; 
} 
+0

仍然沒有工作。 –

+0

最後,我已將我的需求履行轉換爲ckeditor –