如何更改带有文本 <input> 框的 window.location 网址

How to change window.location urls with text <input> boxes

好的,我正在制作视频游戏,我希望玩家能够通过文本输入框在游戏中 select 自定义文件。我能够让一切正常工作,但是当我单击加载级别按钮时,它将带我到:

.../自定义Levels/undefined.html 我尝试了其他一些方法,但我唯一得到的是:

.../自定义级别/[对象 HTMLInputElement].html

<div>
    <p><input type="text" id="LVLNK" value="Level Name"></p>
    <p><input type="button" id="MButton" value="Load Level" onclick="LDLVLNK()"></p>
    </div>

<script>
var time = setInterval(10,gameLoop)
var LevelLink; /*put: "var LevelLink = document.getElementById("LVLNK");" to get other link example instead*/

function gameLoop()
{
LevelLink = document.getElementById("LVLNK").value;
}

function LDLVLNK()
{
window.location = "../Custom Levels/" + LevelLink + ".html";
}

我当前尝试访问的文件名为 "CLevel1"

所以我会把 CLevel1 放在输入框中。 gameLoop 会将名称设置为 LevelLink 变量,然后将其添加到位于由按钮激活的 LDLVLNK 函数内部的 window.location 函数中的完整 link。

我想你可以尝试以下方法:

function LDLVLNK(){
  location.href = "../Custom Levels/" + LevelLink + ".html";
//here window is optional, location is a globally accessible object
}



顺便说一句:
你弄错了 setInterval,你应该使用 setInterval(gameLoop, 10);(即使 10 对浏览器来说可能不是很 good/maintainable,尝试 100,如果太多的话,通过触摸减少数量)

你把 setInterval() 的论据反过来,应该是:

setInterval(gameLoop, 10);

但是 LevelLink 全局变量没有理由。您可以简单地在 LDLVLNK():

中获取输入值
function LDLVLNK() {
    var LevelLink = document.getElementById("LVLNK").value;
    if (LevelLink) {
        window.location = "../Custom Levels/" + LevelLink + ".html";
    } else {
        alert("Nowhere to go to");
    }
}