我有两个单独的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'
:
请帮我解决错误。
答案 0 :(得分:4)
我很确定这是罪魁祸首:
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
可能应该以字符串开头的基本问题。
答案 1 :(得分:0)
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