代码之家  ›  专栏  ›  技术社区  ›  SearchForKnowledge

为什么我在输入文本区域时收到替换函数错误

  •  0
  • SearchForKnowledge  · 技术社区  · 9 年前

    我有两个单独的jquery弹出窗口,其中一个是从 GridView 数据:

    //THIS IS THE POPUP WHICH PRE POPULATES THE TEXTAREA VALUE FROM THE GRIDVIEW
    <asp:UpdatePanel runat="server" ID="upPop" UpdateMode="Conditional">
        <ContentTemplate>
            <div id="popupContactEM">
                <a id="popupContactCloseEM" title="Close Window">x</a>
                <div id="dvFourthEM" class="mainFirst">
                    <div id="leftdiv1EM" class="leftdivspec"><sup style="color: #FF0000; font-weight: bold;">*</sup>Message:<br /><br /><b style="font-size: xx-small;">Character Count: </b><input maxlength="4" size="4" id="charCountE" style="border: none; font-size: xx-small; color: #E55302;" disabled="disabled" /><br /><b style="font-size: xx-small;">Word Count: </b><input maxlength="4" size="4" style="border: none; font-size: xx-small; color: #E55302;" id="wordCountE" disabled="disabled" /></div>
                    <div id="rightdiv1EM" class="rightdivspec"><asp:TextBox ID="tbMessageEM" ClientIDMode="Static" runat="server" TextMode="MultiLine" Columns="30" Rows="5"></asp:TextBox></div>
                </div>
            </div>
            <div id="backgroundPopupEM"></div>
        </ContentTemplate>
    </asp:UpdatePanel>
    
    <div id="popupContact">
        <a id="popupContactClose" title="Close Window">x</a>
        <div id="dvFourth" class="mainFirst">
            <div id="leftdiv1" class="leftdivspec"><sup style="color: #FF0000; font-weight: bold;">*</sup>Message:<br /><br /><b style="font-size: xx-small;">Character Count: </b><input maxlength="4" size="4" id="charCount" style="border: none; font-size: xx-small; color: #E55302;" disabled="disabled" /><br /><b style="font-size: xx-small;">Word Count: </b><input maxlength="4" size="4" style="border: none; font-size: xx-small; color: #E55302;" id="wordCount" disabled="disabled" /></div>
            <div id="rightdiv1" class="rightdivspec"><asp:TextBox ID="tbMessage" ClientIDMode="Static" runat="server" TextMode="MultiLine" Columns="30" Rows="5"></asp:TextBox></div>
        </div>
    </div>
    <div id="backgroundPopup"></div>
    

    我尝试用显示任一弹出窗口的WORD和CHARACTER计数,我有以下JQuery:

    $(document).ready(function () {
        $("#tbMessage").keyup(function () {
            WordCounter(this, 0);
        });
    
        $("body").on('keyup', "#tbMessageEM", function (e) {
            WordCounter(this, 1);
        });
    });
    
    function WordCounter(txt, whc) {
    
        if (whc == 0) {
            s = txt.value;
        }
        else {
            s = txt;
        }
        if (s.length == 0) {
            len = 0;
        }
        else {
            m1 = s.replace(/\s/g, '+');
            m = m1.replace(/^\s/g, '+');
            len = countWords(m);
        }
        if (whc == 0) {
            document.getElementById("charCount").value = s.length;
            document.getElementById("wordCount").value = len;
        }
        else {
            document.getElementById("charCountE").value = s.length;
            document.getElementById("wordCountE").value = len;
        }
    
    }
    function countWords(str) {
    
        str1 = str.replace(/\+*$/gi, "");
        str2 = str1.replace(/\++/g, ' ');
        var temp = new Array();
        temp = str2.split(' ');
        return temp.length;
    }
    

    当未预先填充的弹出窗口打开时,我可以在 TextArea 它给了我计数。

    当预填充的弹出窗口打开时,它会根据 文本域 但当我去输入或删除任何内容时,会出现以下错误: 0x800a01b6 - JavaScript runtime error: Object doesn't support property or method 'replace' :

    enter image description here

    请帮我解决这个错误。

    2 回复  |  直到 9 年前
        1
  •  4
  •   KJ Price    9 年前

    我很确定这就是罪魁祸首:

    if (whc == 0) {
        s = txt.value;
    }
    else {
        s = txt;//This is a Node object
    }
    ...
    m1 = s.replace(/\s/g, '+');
    

    你有时试图得到 .replace 从不存在的HTML元素对象中删除。

    编辑: 我不完全确定你希望在这里实现什么。最明显的修复方法是确保正在使用字符串,从而避免调用 代替 在节点对象上,向 if 声明:

    if (!s.replace || s.length == 0) {
    

    尽管如此,这可能仍然忽略了潜在的问题 s 可能应该以字符串开头。

        2
  •  0
  •   SearchForKnowledge    9 年前

    txt 即将到来 [object HTML TextAreaElement] .

    我将代码更改为:

    if (whc == 0) {
        s = txt.value;
    }
    else {
        s = $("#tbMessageEM").val();;
    }
    

    而不是 文本 ,我得到了文本区域的值,它工作正常。

    为什么不起作用 :

    对于预先填充的文本区域,我是这样加载弹出窗口的:

    function loadPopupEM() {
        //loads popup only if it is disabled
        if (popupStatusEM == 0) {
            $("#backgroundPopupEM").css({
                "opacity": "0.7"
            });
            $("#backgroundPopupEM").fadeIn("slow");
            $("#popupContactEM").fadeIn("slow");
            popupStatusEM = 1;
            var w = $("#tbMessageEM").val(); //get the prepopulated text
            WordCounter(w, 1);
        }
    }
    

    当弹出窗口加载时,我在行中出现以下错误 if (s.length == 0) :

    0x800a138f - JavaScript runtime error: Unable to get property 'length' of undefined or null reference