gpt4 book ai didi

javascript - 将 QR 码值扫描到输入字段中

转载 作者:行者123 更新时间:2023-12-03 02:38:33 25 4
gpt4 key购买 nike

这是我正在使用的扫描仪...

网上:https://atandrastoth.co.uk/main/pages/plugins/webcodecamjs/

在 Git 上:https://github.com/andrastoth/WebCodeCamJS

100% 正常工作。但我需要添加一些自定义的额外内容。

扫描二维码后,会将结果输出到此段落标签中。

<p id="scanned-QR"> The scanned code text value is printed out here </p>

但是我需要它作为输入字段,以便我可以在网址中使用它的值。

如何将输入字段设置为等于提交给段落标记的值?

我尝试过这些方法,但都失败了:

方法一

<input id="scanned-QR"> The scanned code text value is printed out here </input>

方法2

<p id="scanned-QR" onchange="update"></p>
<input id="code_id_value" type="text" name="" value="">
<br>

<script>
function update(){
var code_id_value = document.getElementById("scanned-QR").innertext;
document.getElementById("code_id_value").value = code_id_value;
}
</script>

最佳答案

您缺少的关键是 .innertext 中的 T 需要大写(如 .innerText )。

除此之外,使用内联事件处理程序是不好的做法,您应该考虑使用 .addEventListener()而不是 onchange

这可以在以下情况中看到:

document.getElementById("scanned-QR").addEventListener("click", update);

function update() {
var code_id_value = document.getElementById("scanned-QR").innerText;
document.getElementById("code_id_value").value = code_id_value;
}

// Demo
update();
<p id="scanned-QR">Text</p>
<input id="code_id_value" type="text" name="" value="">

希望这有帮助! :)

关于javascript - 将 QR 码值扫描到输入字段中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48433928/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com