2010-08-10 32 views
1

我正在通過$ .get()加載HTML的外部塊,我想將字符計數器綁定到在其中加載的任何textareas。使用帶有textarea字符計數器的live()或livequery()

通常對於這種類型的任務,如果它是基於事件的,則使用live(),如果是自定義函數,則使用livequery插件。因爲我一直在使用字符計數器插件,所以我一直在使用livequery,但是我不能在我的生活中讓char計數器在新創建的textareas上工作。不知道在我的livequery嘗試上粘貼很多點,但在這裏它是無論如何!

$("textarea").livequery(function() { 
    $(this).charcounter(); //or whatever the name of the counter function is 
}); 

我已經試過了幾個櫃檯的jQuery插件,其中有兩個我在這裏鏈接:

http://plugins.jquery.com/project/TextareaCharactersCounter http://brandonaaron.net/code/countable/docs

到目前爲止沒有奏效。代碼/函數對尚未通過jQuery $ .load,$ .get或$ .post加載的內容起作用。

有人嗎?我正在向這裏的頭髮撕開舞臺!

jQuery代碼,這在了slideDown()調用之前存在的斷裂,由於參與$(數據)的東西.find ...行:

$.get("lib/scripts/php/ajax/create-content-forms.php", { typeId : typeId }, function(data){ 


       var options = { 
        'maxCharacterSize': 200, 
        'textFontSize': '12px', 
        'textColor': '#5C2306', 
        'textFamily': 'Tahoma,sans-serif', 
        'textAlign': 'right', 
        'warningColor': '#CC3300', 
        'warningNumber': 40, 
        'isCharacterCount': true, 
        'isWordCount': false 
       }; 

       $(data).find('textarea').textareaCount(options).end().appendTo("#contentFormContent"); 
       $("#contentForm").slideDown(); 

      }); 

和HTML的相關片段,是生產的:

<fieldset> 
    <legend>Additional information:</legend> 
    <p> 
     <span class="fieldtext">Description:</span> 
     <textarea name="description" id="description" class="charLimitShort"><?php echo $description ?></textarea> 
    </p> 
</fieldset> 

回答

0

我唯一能想到的是data不是你想象的。

試試這個:

$.get("lib/scripts/php/ajax/create-content-forms.php", { typeId : typeId }, function(data) { 


    var options = { 
     'maxCharacterSize': 200, 
     'textFontSize': '12px', 
     'textColor': '#5C2306', 
     'textFamily': 'Tahoma,sans-serif', 
     'textAlign': 'right', 
     'warningColor': '#CC3300', 
     'warningNumber': 40, 
     'isCharacterCount': true, 
     'isWordCount': false 
    }; 

    var myData = '<fieldset>' + 
      '<legend>Additional information:</legend>' + 
      '<p>' + 
      '<span class="fieldtext">Description:</span>' + 
      '<textarea name="description" id="description" class="charLimitShort">Hello</textarea>' + 
      '</p>' + 
      '</fieldset>'; 

    $(myData).find('textarea').textareaCount(options).end().appendTo("#contentFormContent"); 
    $("#contentForm").slideDown(); 

}); 

如果該工程確定,那麼你知道它是數據(服務器)。

+0

不幸的是,不幸的是,也沒有工作!某處出了問題 - slideDown()調用沒有發生。 在檢查Firefox的錯誤控制檯,它似乎仍然產生 「錯誤:$(MYDATA的).find(」錯誤 我開始認爲這是什麼做的textareaCount插件。 – user416330 2010-08-11 10:12:12

相關問題