将表单值传递给全局变量
尝试将表单值传递到全局 var 中以在两个函数中使用,但出现此错误 document.PostReply 未定义
或 javascript 崩溃,因为 getReplyId 在该函数中未定义功能。无论我尝试了什么,我都会遇到一个或其他错误。
任何人都可以明白为什么表单值“ReplyID”没有被传递到 var 中。
var 放置在脚本开头的任何函数之前。
表单
<CFFORM NAME="PostReply" ID="PostReply" METHOD="POST">
<CFINPUT TYPE="hidden" NAME="ReplyID" ID="ReplyID" VALUE="#ReplyID#">
</CFFORM>
我尝试过的
1 - var getReplyId = document.PostReply.ReplyID;
2 - var getReplyId = document.PostReply.ReplyID.value;
3 - var getReplyId = document.getElementById("ReplyID");
4 - var getReplyId = document.PostReply.getElementById("ReplyID");
5 - var getReplyId = document.getElementById("PostReply.ReplyID").value; `
如果我只是这样做 var getReplyId = 1
脚本效果很好,但我需要 ReplyID 值。
好的,这里是大部分内容
<CFOUTPUT>
<STYLE>
targetPostReply#ReplyID#{visibility : visible}
targetPostReplyResponse#ReplyID#{visibility : visible}
</STYLE>
</CFOUTPUT>
<cfajaxproxy cfc="CFC/PostReply" jsclassname="PostReplyCFC">
<SCRIPT SRC="js/PostReply.js" TYPE="text/javascript">
var getReplyId = document.getElementById("ReplyID").value;
$(document).ready(
function () {
$("form#PostReply").submit(
function PostNewReply(ReplyID, ReplyComment){
var cfc = new PostReplyCFC();
cfc.setCallbackHandler(getNewReply)
cfc.setForm('PostReply')
cfc.PostNewReply('ReplyComment');
document.getElementById("targetPostReplyResponse"+getReplyId).style.display='block';
document.getElementById("targetMakeReply"+getReplyId);
$('#targetMakeReply'+getReplyId).slideToggle("slow");
$(this).toggleClass("targetMakeReply"+getReplyId);
return false;
}); // .submit()
});
function getNewReply(newReply)
{
$('#replyResponse'+getReplyId).html(newReply);
return false;
}
</SCRIPT>
<CFOUTPUT>
<DIV ID="targetMakeReply#ReplyID#">
<CFFORM NAME="PostReply" ID="PostReply" METHOD="POST">
<CFINPUT TYPE="hidden" NAME="ReplyID" ID="ReplyID" VALUE="#ReplyID#">
<textarea name="ReplyComment" rows="5" cols="95"></textarea>
<CFINPUT type="image" name="Submit" id="submitButton" value="Post Reply" src="images/PostReply.gif" onmouseover="src='images/PostReplyO.gif'" onmouseout="src='images/PostReply.gif'">
</CFFORM>
</DIV>
</CFOUTPUT>
<CFOUTPUT>
<STYLE>##targetPostReplyResponse#ReplyID#{display : none}</STYLE>
<DIV ID="targetPostReplyResponse#ReplyID#">
<TABLE ALIGN="center" VALIGN="top" WIDTH="750" BGCOLOR="FFFFFF" CELLPADDING="0" CELLSPACING="0">
<TR>
<TD COLSPAN="2" CLASS="text12B"><IMG SRC="images/blank.gif" WIDTH="1" HEIGHT="5" ALT="" BORDER="0"><BR><SPAN ID="replyResponse#ReplyID#"></SPAN></TD>
</TR>
</TABLE>
</DIV>
</CFOUTPUT>
Trying to pass a form value into a global var to be use in two functions but get this error document.PostReply is undefined
or the javascript just crashes because getReplyId is undefined within the functions. No matter what I have tried I get one or the other errors
Can anyone see why the form value "ReplyID" is not being passed into the var.
The var is placed at the begining of the script before any functions.
form
<CFFORM NAME="PostReply" ID="PostReply" METHOD="POST">
<CFINPUT TYPE="hidden" NAME="ReplyID" ID="ReplyID" VALUE="#ReplyID#">
</CFFORM>
what I have tryed
1 - var getReplyId = document.PostReply.ReplyID;
2 - var getReplyId = document.PostReply.ReplyID.value;
3 - var getReplyId = document.getElementById("ReplyID");
4 - var getReplyId = document.PostReply.getElementById("ReplyID");
5 - var getReplyId = document.getElementById("PostReply.ReplyID").value; `
If I just do this var getReplyId = 1
the script works great, but I need the ReplyID value.
Ok here is everything for the most part
<CFOUTPUT>
<STYLE>
targetPostReply#ReplyID#{visibility : visible}
targetPostReplyResponse#ReplyID#{visibility : visible}
</STYLE>
</CFOUTPUT>
<cfajaxproxy cfc="CFC/PostReply" jsclassname="PostReplyCFC">
<SCRIPT SRC="js/PostReply.js" TYPE="text/javascript">
var getReplyId = document.getElementById("ReplyID").value;
$(document).ready(
function () {
$("form#PostReply").submit(
function PostNewReply(ReplyID, ReplyComment){
var cfc = new PostReplyCFC();
cfc.setCallbackHandler(getNewReply)
cfc.setForm('PostReply')
cfc.PostNewReply('ReplyComment');
document.getElementById("targetPostReplyResponse"+getReplyId).style.display='block';
document.getElementById("targetMakeReply"+getReplyId);
$('#targetMakeReply'+getReplyId).slideToggle("slow");
$(this).toggleClass("targetMakeReply"+getReplyId);
return false;
}); // .submit()
});
function getNewReply(newReply)
{
$('#replyResponse'+getReplyId).html(newReply);
return false;
}
</SCRIPT>
<CFOUTPUT>
<DIV ID="targetMakeReply#ReplyID#">
<CFFORM NAME="PostReply" ID="PostReply" METHOD="POST">
<CFINPUT TYPE="hidden" NAME="ReplyID" ID="ReplyID" VALUE="#ReplyID#">
<textarea name="ReplyComment" rows="5" cols="95"></textarea>
<CFINPUT type="image" name="Submit" id="submitButton" value="Post Reply" src="images/PostReply.gif" onmouseover="src='images/PostReplyO.gif'" onmouseout="src='images/PostReply.gif'">
</CFFORM>
</DIV>
</CFOUTPUT>
<CFOUTPUT>
<STYLE>##targetPostReplyResponse#ReplyID#{display : none}</STYLE>
<DIV ID="targetPostReplyResponse#ReplyID#">
<TABLE ALIGN="center" VALIGN="top" WIDTH="750" BGCOLOR="FFFFFF" CELLPADDING="0" CELLSPACING="0">
<TR>
<TD COLSPAN="2" CLASS="text12B"><IMG SRC="images/blank.gif" WIDTH="1" HEIGHT="5" ALT="" BORDER="0"><BR><SPAN ID="replyResponse#ReplyID#"></SPAN></TD>
</TR>
</TABLE>
</DIV>
</CFOUTPUT>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要使变量成为全局变量,只需去掉
var
即可。但请注意,全局变量通常不是最佳实践。如果可能,请考虑让一个函数调用另一个函数并将变量作为参数传递。
To make a variable global, just leave away the
var
.note though that global variables are often sub-optimal practice. If possible, consider having one function call the other and pass the variable as a parameter.
好吧,我不知道为什么会这样,但我将
getReplyId = document.getElementById("ReplyID").value;
放在 PostNewReply 函数中,它仍然作为全局变量工作。据我所知,这不应该起作用,但它确实起作用。Well i dont know why this works but i put
getReplyId = document.getElementById("ReplyID").value;
inside the PostNewReply function and it still works as a global var. From everything I know this shouldn't work but it does.将所有 JS 封装在一个新函数调用“SubmitReply”中,并通过 INPUT Submit 按钮中的 onclick“SubmitReply”将 ID 传递给它。然后,ID (ReplyID) 被传递到
var getReplyId = (Id)
。这使得 getReplyId 能够被上面显示的所有 JS 使用。现在,JS、cfajaxproxy 和 CFC 可以使用 ID,使表单具有自己的 ID 动态。Wrapped all the JS inside a new function call "SubmitReply" and passed the ID to it with an onclick "SubmitReply" in the INPUT Submit button. The ID (ReplyID) was then passed to a
var getReplyId = (Id)
. This made the getReplyId use able by all the JS shown above. Now the JS, cfajaxproxy, and CFC can use the ID making the form dynamic with their own ID.