我的 javascript 变量在函数调用之间变得未定义
My javascript variable is becoming undefined between function calls
我有一个表单,当按下链接按钮时,它会调用一些javascript和一些隐藏的代码(对我来说是C#)。正常操作应如下所示:单击链接按钮,从表单上的标签中获取文本,打开HTML编辑器,通过单击文本编辑器上的"完成"来保存文本,然后再次在表单上显示文本。它将完成所有这些操作,但是我收到一个错误(控件未定义),然后阻止我在表单上执行任何其他工作。
首先,这是我的 aspx 代码,它是一个保存 HTML 编辑器和 HTML 编辑器的窗口。
<asp:Panel ID="EditCommentPopup" runat="server" SkinID="PopupPanel" HorizontalAlign="Center" Width="500px" Style="display:none;">
<div id="SendCommentHeader" class="modalPopupHeader">
<table width="100%" style="vertical-align: middle">
<tr>
<td style="width: 50%; text-align: left; padding-left: 5px;">
<asp:Label ID="EditCommentHeaderLBL" runat="server" Font-Bold="true" Text="Change Comment" ></asp:Label>
</td>
<td style="width: 50%; text-align: right; padding-right: 3px;">
<asp:Button SkinID="SubmitButton" ID="SaveComment" runat="server" Text="Done" OnClientClick="submitEditorPopup(); return true;" OnClick="SubmitCommentButton_Click"/>
<asp:Button SkinID="CancelButton" ID="cancelCommentButton" runat="server" Text="Cancel"/>
</td>
</tr>
</table>
</div>
<ajaxToolkit:ModalPopupExtender ID="EditCommentPopupExtender" runat="server" TargetControlID="EditCommentHeaderLBL"
PopupControlID="EditCommentPopup" BackgroundCssClass="modalBackground" PopupDragHandleControlID="EditCommentPopup"
DropShadow="False" CancelControlID="cancelCommentButton" BehaviorID="ChangeCommentsPopup" >
</ajaxToolkit:ModalPopupExtender>
<asp:Panel ID="EditCommentInnerPanel" SkinID="PopupInnerPanel" HorizontalAlign="Center" runat="server" >
<asp:Label ID="CommandArgLBL" runat="server" Visible="false"></asp:Label>
<custom:CustomTextEditor ID="EditCommentsTextBox" runat="server" Width="100%" Height="300px" NoScript="true" />
</asp:Panel>
</asp:Panel>
这是我的JavaScript函数
var contentLBL;
var editorControl;
var hiddenField;
function LoadEditorPopup(labelID, editorID, hiddenID, linkID) {
editorControl = document.getElementById(editorID);
contentLBL = document.getElementById(labelID);
hiddenField = document.getElementById(hiddenID);
editorControl.control.set_content(contentLBL.innerHTML);
var popup = $find("ChangeCommentsPopup");
popup.show();
}
function submitEditorPopup() {
var tmp = editorControl.control.get_content(); //error flags here
contentLBL.innerHTML = tmp;
var str= new String();
str = tmp;
str = str.replace(/</g, "{%");
str = str.replace(/>/g, "%}"); //stripping out html tags to prevent sql insertion
hiddenField.value = str;
var popup = $find("ChangeCommentsPopup");
popup.hide();
}
这是我的 C# 函数
protected void ShowEditCommentPopup(object sender, EventArgs e)
{
string arg = ((LinkButton)sender).CommandArgument;
string content = "";
string title = "";
switch (arg)
{
//doing stuff, setting content and title
}
EditCommentHeaderLBL.Text = title;
CommandArgLBL.Text = arg;
EditCommentsTextBox.Content = content;
EditCommentPopupExtender.Show();
EditModalPopupExtender.Show(); //this is the form
}
所以,这里发生了什么,我点击一个链接按钮(未显示在aspx代码中),它会在javascript中触发LoadEditorPopup函数。它完成了它的工作,我在 onclientclick 调用中有一个"return true",以便调用代码隐藏中的下一个函数 ShowEditCommentPopup。编辑器弹出,我可以很好地使用它。当我单击"完成"时,SubmitEditorPopup事件在javascript中触发,我收到一个错误,指出变量EditorControl未定义。我发现如果我不调用 ShowEditCommentPopup 方法,则不会发生错误,但随后我又回到了我最初遇到的另一个问题,我希望如果这个问题得到解决,这个问题将得到解决。
我真的希望这一点足够清楚。提前感谢!
你有一个包含 JavaScript 函数的代码片段。该代码位于何处?它在可以第二次调用的类或函数中吗?如果是这样,var editorControl;
可能会再次运行并覆盖您之前的 editorControl 变量。我会对var editorControl;
发出警报以确保。
alert( 'Should only happen once' );
var editorControl;
由于这没有帮助,我想您可以尝试更换
var tmp = editorControl.control.get_content();
跟
tmp = document.getElementById(editorID);
在提交编辑器弹出窗口中。
- 为什么“;未定义的“;在JavaScript中结束循环
- 要求未定义JS回调参数
- 如何检查管道中未定义的项目
- TypeError:无法读取属性'推'未定义的JavaScript
- $window.ga在AngularJS事件中未定义
- Javascript 类型未定义和 void 之间的区别
- JS:未定义值和未定义类型,以及null值和null类型之间的区别
- 尝试在组件之间传递数据时未定义
- 未捕获的引用错误和未定义之间有什么区别
- 返回承诺与返回承诺内未定义之间的区别
- 确定猫鼬中空数组和未定义数组之间的区别
- 我的 javascript 变量在函数调用之间变得未定义
- a = [未定义] 和 a = new Array(1) 之间的区别
- 两点之间的距离谷歌地图a是未定义的api v3
- 检查未定义和null的不等式(!==)和检查未定义和null的柯夫不等式(!=)之间有区别吗?
- 路由未定义在谷歌地图API,而发现两个地方之间的持续时间
- 无效的列索引未定义.取值范围为[0-8]之间的整数
- 什么'Array(2)和[未定义,未定义]之间的区别是什么
- JS:未定义和“未定义”之间的区别
- 在中间件之间正确传递数据会记录,但返回未定义的内部对象