Stack 的 friend 们,大家好
我的代码有一个小问题。
我尝试使用 window.location.href
发送我的变量,但我不断收到错误未定义
。
这是选择我的图像 (src) 和 id (chosenid) 的函数 正如我测试的那样, selectedid 确实有效。
function HeadShow() {
var HeadItems = document.getElementById('HeadItems');
HeadItems.style.display = 'block';
}
function headHide(src, chosenid) {
var head = document.getElementById("head");
var HeadItems = document.getElementById('HeadItems');
HeadItems.style.display = 'none';
head.innerHTML = "<img src='" + src + "' width=80; height=80;>";
var chosenhead = chosenid;
}
然后,我还有将变量发送到下一页的函数:
function send() {
window.location.href = "test3.php?head=" + chosenhead;
}
我得到的只是错误:chosenhead is not Defined
希望大家能帮我解决这个问题。
最佳答案
只需声明变量 chosenhead
全局。还有你的head.innerHTML
分配不正确。
示例代码
var chosenhead;
function headHide(src, chosenid) {
var head = document.getElementById("head");
var HeadItems = document.getElementById('HeadItems');
HeadItems.style.display = 'none';
head.innerHTML = "<img src='" + src + "' style='width=80; height=80;'>";
chosenhead = chosenid;
}
function send() {
window.location.href = "test3.php?head=" + chosenhead;
}
如果只使用参数 chosenid
是不是会好很多?通过 function send()
?它看起来像一个全局变量。
关于javascript - 从函数到 window.location.href 的变量 'not defined',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21479467/