在JavaScript中维护变量值的困难
我用javascript编写了两个单独的函数,并创建了一个全局变量。 第一个函数设置全局变量的值,第二个函数使用该全局变量检查条件,但它不起作用 这是我的密码在JavaScript中维护变量值的困难,javascript,global-variables,Javascript,Global Variables,我用javascript编写了两个单独的函数,并创建了一个全局变量。 第一个函数设置全局变量的值,第二个函数使用该全局变量检查条件,但它不起作用 这是我的密码 var flag = 1; function setSelection(){ for (index=0; index < top.parent.frmRadio.view.length; index++) { if (top.parent.frmRadio.view[index].checked) {
var flag = 1;
function setSelection(){
for (index=0; index < top.parent.frmRadio.view.length; index++) {
if (top.parent.frmRadio.view[index].checked) {
var radioValue = top.parent.frmRadio.view[index].value;
if(radioValue == "graph"){
flag = 1;
top.parent.test2.innerHTML = flag;
}
else{
flag = 0;
top.parent.test2.innerHTML = flag;
}
}
}
}
function setFileName(name){
var fileName = name;
// document.getElementById("body").innerHTML = fileName;
document.getElementById("body").innerHTML = flag;
if(flag == 1){
top.parent.frame2.location = fileName;
document.getElementById("body").innerHTML = fileName;
}
else{
top.parent.frame2.location = "simpletree.html";
document.getElementById("body").innerHTML = "simpletree.html";
}
// parent.frame2.location = fileName;
}
var标志=1;
函数setSelection(){
对于(索引=0;索引
这两个函数都由不同的位置调用。单击单选按钮时调用第一个方法,单击列表时调用第二个方法。您的全局变量没有问题(如果全局变量没有问题,强烈建议避免使用它们) 我认为问题出在
setFileName
函数中,这里有一些看起来很奇怪的操作。一些评论:
function setFileName(name){
var fileName = name;
// document.getElementById("body").innerHTML = fileName;
document.getElementById("body").innerHTML = flag;
// ^-- Do you *really* have an element on the page with the id="body"?
// This will not change the `body` element of the page (unless you've given it that id).
if(flag == 1){
top.parent.frame2.location = fileName;
document.getElementById("body").innerHTML = fileName;
// ^--- This will replace the content of the element with id="body" with
// the text of the filename (it will not retrieve the file)
}
else{
top.parent.frame2.location = "simpletree.html";
document.getElementById("body").innerHTML = "simpletree.html";
// ^--- This will replace the contents of the element with id="body"
// with the text "simpletree.html", it will not retrieve the file
}
// parent.frame2.location = fileName;
}
设置元素的
innerHTML
会将其内容更改为您指定的实际文本。它不会加载页面并将页面内容放在那里。为此,您必须使用iframe
或使用ajax加载页面内容(来自同一来源的服务器),然后分配生成的文本/标记。“但它不起作用”怎么办?你期望得到什么结果?你得到了什么结果?您是否在JavaScript控制台中看到任何错误?当你使用调试器浏览它时,你会看到什么?我使用这个“innerHTML”只是为了检查它是否正确input@Ashish:其他几点呢?特别是,您真的有一个id为“body”的元素吗?@T.J.Crowder是的,但这只是为了测试目的,当它工作正常时,我将删除它。