2014-03-07 319 views
0

我試圖把字符留在textarea中。但它無法工作。我的腳本:TinyMCE字符剩餘

<script type="text/javascript"> 
function updateCountdown() { 
    // 140 is the max message length 
    var remaining = 300 - jQuery('.short').val().length; 
    jQuery('.countdown').text(remaining + ' characters remaining.'); 
} 

jQuery(document).ready(function($) { 
    updateCountdown(); 
    $('.short').change(updateCountdown); 
    $('.short').keyup(updateCountdown); 
}); 
</script> 
<label>Short message <span class="countdown"></span></label> 
<?php echo form_textarea('short_content', '', 'class="short"'); ?> 

我使用了tinymce編輯器。它只在我重新播放頁面時起作用。誰能幫忙?

我add.php文件看起來像這樣:

<script type="text/javascript"> 
function updateCountdown(remaining_text) { 
    // 140 is the max message length 
    var remaining = 300 - remaining_text.length; 
    jQuery('.countdown').text(remaining + ' characters remaining.'); 
} 
</script> 
       <label>short content <span class="countdown"></span></label> 
       <?php echo form_textarea('short_content', '', 'class="short"'); ?> 

我的TinyMCE的初始化文件看起來像這樣:

var short = $('textarea.short'); 
    if(short.length > 0) { 
     short.tinymce({ 
      height: 150, 
      language : 'lt', 
      script_url : 'js/tiny_mce/tiny_mce.js', 
      theme : 'advanced', 
      skin : 'wp_theme', 
      theme_advanced_toolbar_location : 'top', 
      theme_advanced_toolbar_align : 'left', 
      theme_advanced_resizing : true, 
      plugins : "maxchars,autolink,lists,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,advlist,wordcount", 
      theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,link,unlink,anchor,|,bullist,numlist", 
      theme_advanced_buttons2 : "", 
      theme_advanced_buttons3 : "", 
      theme_advanced_statusbar_location : "bottom", 
      max_chars : 250, 
max_chars_indicator : "lengthBox", 
      toolbar: "wordcount", 
       force_br_newlines : true, 
     force_p_newlines : false, 
      forced_root_block : false, 

      file_browser_callback : "tinyBrowser", 

      setup: function(ed){ 
       ed.onInit.add(function(ed) { 
        ed.pasteAsPlainText = true; 
       }); 
       ed.onPaste.add(function(ed) { 
        ed.pasteAsPlainText = true; 
       }); 

      }, 
        }); 
    } 
}); 
+0

[您的代碼似乎工作](http://jsfiddle.net/andyuws/YwLvJ/)。還有其他一些事情正在發生。 – Andy

+0

是的,我知道它,但它只是當我reloud頁面時工作。也許這是關於以下幾點的問題:重點? – killah

回答

0

val()不會得到你的TinyMCE的編輯器的價值。 tinyMCE.get('content id').getContent()會。此外,事件在tinymce api中指定。 tinymce基於您的代碼示例:

<script type="text/javascript"> 
    function updateCountdown(remaining_text) { 
     // 140 is the max message length 
     var remaining = 300 - remaining_text.length; 
     jQuery('.countdown').text(remaining + ' characters remaining.'); 
    } 

    var short = $('textarea.short'); 
    short.tinymce({ 
     height: 150, 
     language : 'lt', 
     script_url : 'js/tiny_mce/tiny_mce.js', 
     theme : 'advanced', 
     skin : 'wp_theme', 
     theme_advanced_toolbar_location : 'top', 
     theme_advanced_toolbar_align : 'left', 
     theme_advanced_resizing : true, 
     plugins : "maxchars,autolink,lists,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,advlist,wordcount", 
     theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,link,unlink,anchor,|,bullist,numlist", 
     theme_advanced_buttons2 : "", 
     theme_advanced_buttons3 : "", 
     theme_advanced_statusbar_location : "bottom", 
     max_chars : 250, 
     max_chars_indicator : "lengthBox", 
     toolbar: "wordcount", 
     force_br_newlines : true, 
     force_p_newlines : false, 
     forced_root_block : false, 

     file_browser_callback : "tinyBrowser", 

     setup: function(ed){ 
      ed.onChange.add(function(ed, l) { 
       if(l.content.length > 0) updateCountdown(l.content);    
      }); 
      ed.onKeyDown.add(function(ed, l) { 
       if(ed.getContent().length > 0) updateCountdown(ed.getContent());    
      });    
      ed.onInit.add(function(ed) { 
       ed.pasteAsPlainText = true; 
      }); 
      ed.onPaste.add(function(ed) { 
       ed.pasteAsPlainText = true; 
      });   
     }, 
    }); 
}); 
</script> 
+0

可以發佈完整的代碼嗎? – killah

+0

發佈,沒有測試它,但應該沒問題。 – sunshinejr

+0

我嘗試過,但它沒有工作。我編輯了我的第一篇文章,檢查它!並感謝您的回覆 – killah