Javascript - window.location.assign 不工作 [英] Javascript - window.location.assign not working

查看:24
本文介绍了Javascript - window.location.assign 不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目,我扫描 QR 码,然后它会根据扫描的代码自动打印信息.

I've a project where I scan a QR code, and it will then automatically print out information based on which code was scanned.

我有扫描仪和打印机.每个二维码对应一个网址.我可以让扫描仪填充输入字段,并且我可以收到警报以显示已扫描的信息.但是,每当我尝试分配 URL 时,它只是将信息附加到当前 URL,而不是替换它.

I have the scanner, and a printer. Each QR code is to correspond to a URL. I can get the scanner to fill an input field, and I can get an alert to show the information that was scanned. However, whenever I try to assign the URL it simply appends the information to the current URL, instead of replacing it.

我需要它来替换当前的 URL.有谁知道它为什么这样做,以及如何解决它?

I need it to replace the current URL. Does anyone know why it is doing this, and how to fix it?

<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>

推荐答案

看起来表单正在提交.取消它.

Looks like the form is submitting. Cancel it.

<form name="myform" onsubmit="return false">

这篇关于Javascript - window.location.assign 不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆