为什么我在输入文本区域时会收到替换功能错误
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
内的文本中给出了 word/character 计数,但是当我去输入或删除任何内容时,我收到以下错误: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 的值,它工作正常。
为什么它不起作用:
对于预填充的文本区域,我是这样加载弹出窗口的:
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
我有两个单独的 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
内的文本中给出了 word/character 计数,但是当我去输入或删除任何内容时,我收到以下错误: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 的值,它工作正常。
为什么它不起作用:
对于预填充的文本区域,我是这样加载弹出窗口的:
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