javascript在将id传递给函数时丢失id
在Coldfusion中,程序person1.cfm的代码如下:javascript在将id传递给函数时丢失id,javascript,coldfusion,radio-button,Javascript,Coldfusion,Radio Button,在Coldfusion中,程序person1.cfm的代码如下: <form action = "person2.cfm" id = "pers2form" name = "pers2form" method = "post" onsubmit = "return pers1()"> <table> <td> <input type = "radio"
<form action = "person2.cfm"
id = "pers2form"
name = "pers2form"
method = "post"
onsubmit = "return pers1()">
<table>
<td>
<input type = "radio"
class = "moxradio round"
id = "Person"
name = "whereto"
value = "Person">
</td>
<td style = "color: ##946ca6"> Update Person</td>
<tr>
<td>
<input type = "radio"
class = "moxradio round"
id = "Persactiv"
name = "whereto"
value = "PersActiv">
</td>
<td style = "color: ##946ca6; ">Update Activity </td>
</table>
</form>
<input type = "Submit"
name = "subpers2"
id = "subpers2"
class = "submitbut"
style = "display: block; margin:10px auto 10px auto"
value = "Submit">
<cfif IsDefined('form.whereto')>
<cfset whereto = form.whereto>
</cfif>
<cfinclude template = person1.cfm>
<cfoutput>
<script>
recheck('#whereto#')
function recheck(target) {
alert('target is ' + target);
document.getElementById(target).checked = true;
}
</script>
</cfoutput>
更新人
更新活动
这工作正常,并正确提交给person2.cfm,其代码如下:
<form action = "person2.cfm"
id = "pers2form"
name = "pers2form"
method = "post"
onsubmit = "return pers1()">
<table>
<td>
<input type = "radio"
class = "moxradio round"
id = "Person"
name = "whereto"
value = "Person">
</td>
<td style = "color: ##946ca6"> Update Person</td>
<tr>
<td>
<input type = "radio"
class = "moxradio round"
id = "Persactiv"
name = "whereto"
value = "PersActiv">
</td>
<td style = "color: ##946ca6; ">Update Activity </td>
</table>
</form>
<input type = "Submit"
name = "subpers2"
id = "subpers2"
class = "submitbut"
style = "display: block; margin:10px auto 10px auto"
value = "Submit">
<cfif IsDefined('form.whereto')>
<cfset whereto = form.whereto>
</cfif>
<cfinclude template = person1.cfm>
<cfoutput>
<script>
recheck('#whereto#')
function recheck(target) {
alert('target is ' + target);
document.getElementById(target).checked = true;
}
</script>
</cfoutput>
重新检查(“#去哪里#”)
功能复查(目标){
警报(“目标是”+目标);
document.getElementById(target).checked=true;
}
这应该是把支票放回person1的无线电信箱。但事实并非如此。我在控制台上收到此错误:TypeError:document.getElementById(…)为空[Learn More]person2.cfm:393:1 功能重新检查正在选择正确的目标。既然person1.cfm已经包含在内,那个目标应该就在那个里。但它似乎找不到它 其余的person2.cfm显示正确 对第393行的引用是虚假的,因为没有第393行。空值必须出现在函数“重新检查”中。我到处都做过这样的事情,把值放回输入字段和选择框,一切都正常。但我从来没有用单选按钮做过。也许重新检查一个盒子有什么不同?或者我做了一些我看不见的蠢事
有人能发现我做错了什么吗?第二个单选按钮的id是“peractiv”,但您发送的值是“peractiv”。区分大小写的结果是
getElementById()
找不到id。运行此操作时,“目标是”之后的警报对话框中会打印什么?Where to的值是多少?也许作为输出,您还需要确保在运行recheck()之前加载了页面。警报对话框似乎是正确的——如果不是,那么答案应该是正确的。然而,再次问我,我就遇到了问题——在person1.cfm中,我在第二个按钮中错误地输入了PersActiv的id。所以这毕竟是一个愚蠢的错误。是的,这确实是问题所在。你自己的代码很难校对,我很感激你为我发现了这个小问题。