【问题标题】:using live() or livequery() with a textarea character counter使用带有 textarea 字符计数器的 live() 或 livequery()
【发布时间】:2010-08-10 15:50:03
【问题描述】:

我正在通过 $.get() 加载外部 html 块,并且我想将字符计数器绑定到其中加载的任何文本区域。

通常对于这种任务,如果它是基于事件的,我会使用 live(),如果它是自定义函数,我会使用 livequery 插件。因为我一直在使用字符计数器插件,所以我一直在使用 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() 调用之前中断,因为 $(data).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 的相关 sn-p:

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

【问题讨论】:

    标签: jquery-plugins jquery


    【解决方案1】:

    我唯一能想到的是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();
    
    });
    

    如果一切正常,那么您就知道它是数据(服务器)。

    【讨论】:

    • 不,不幸的是,这也不起作用!某处出了点问题 - slideDown() 调用没有发生。在检查 Firefox 的错误控制台时,它似乎仍然产生错误 'Error: $(myData).find(' 我开始认为这与 textareaCount 插件有关。
    猜你喜欢
    • 1970-01-01
    • 2010-12-25
    • 1970-01-01
    • 2011-05-16
    • 1970-01-01
    • 2011-12-14
    • 1970-01-01
    • 2018-11-03
    • 1970-01-01
    相关资源
    最近更新 更多