[英]Javascript - window.location.assign not working
我有一个项目,我扫描 QR 码,然后它会根据扫描的代码自动打印信息。
我有扫描仪和打印机。 每个二维码对应一个网址。 我可以让扫描仪填充输入字段,并且我可以收到警报以显示已扫描的信息。 但是,每当我尝试分配 URL 时,它只是将信息附加到当前 URL,而不是替换它。
我需要它来替换当前的 URL。 有谁知道它为什么这样做,以及如何解决它?
<html>
<head><title>QR Printing</title></head>
<body onload="document.myform.mytextfield.focus();">
<form name="myform">
<input type="text" name="mytextfield" onchange="checkForm()">
</form>
<script type="text/javascript" language="JavaScript">
function checkForm() {
var field1 = document.forms['myform'].elements['mytextfield'].value;
alert(field1);
window.location.assign(field1);
}
</script>
</body>
</html>
看起来表单正在提交。 取消它。
<form name="myform" onsubmit="return false">
你要:
window.location = field1;
添加返回假; 在 window.location.assign(field1); 之后
<html>
<head><title>QR Printing</title></head>
<body onload="document.myform.mytextfield.focus();">
<form name="myform">
<input type="text" name="mytextfield" onchange="checkForm()">
</form>
<script type="text/javascript" language="JavaScript">
function checkForm() {
var field1 = document.forms['myform'].elements['mytextfield'].value;
alert(field1);
window.location.assign(field1);
return false;
}
</script>
</body>
</html>
在 chrome 浏览器中,这两种解决方案都不适合我。 我已经解决了这个问题:
setTimeout(function () { document.location.href = YourUrlLink }, 500);
希望这对正在寻求 chrome 浏览器问题解决方案的人有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.