簡體   English   中英

為什么在輸入文本區域時出現替換功能錯誤

[英]Why am I receiving a replace function error when typing into a textarea

我有兩個單獨的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輸入文本,它可以計數。

當預填充的彈出窗口打開時,它確實提供了TextArea內文本的單詞/字符計數,但是當我輸入或刪除任何內容時,出現以下錯誤: 0x800a01b6 - JavaScript runtime error: Object doesn't support property or method 'replace'

在此處輸入圖片說明

請幫助我解決錯誤。

我可以肯定這是罪魁禍首:

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

有時您試圖從不存在的HTML元素對象中獲取.replace

編輯:我不完全確定您希望在這里實現什么。 最明顯的解決方法是確保您正在使用字符串,因此為避免在節點對象上調用.replace ,請在if語句中添加另一個檢查:

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

雖然,這可能仍然缺少深層的問題s或許應該開始與一個字符串。

txt即將出現[object HTML TextAreaElement]

我將代碼更改為此:

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

我沒有得到txt ,而是得到了textarea的值,並且可以正常工作。

為什么它不起作用

對於預填充的textarea,我以這種方式加載彈出窗口:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM