I am working on a QR code generator project. UI is ready, but regarding the functionality when I press the button, it does not show the QR image.
我正在做一个二维码生成器项目。用户界面已经准备好了,但是当我按下按钮时,它没有显示QR图像。
I checked the console, there were no errors. Also, I actually saw QR image once or twice, but they disappeared in less than second when I pressed the button. How can I solve this issue?
<script> let imgBox = document.getElementById("imgBox"); let qrImage = document.getElementById("qrImage"); let qrText = document.getElementById("qrText"); let qrSize = document.getElementById("size"); let btn = document.getElementById("btn");
function generateQR() { qrImage.src = "https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=" + qrText.value; imgBox.classList.add("show-img"); if (!qrText.value || qrText.value === "") { alert("Please Enter URL"); } }
<button id="btn" onclick="generateQR()" type="button">Copy Me</button> add type = button in this button element and your problem will be solved.
在这个按钮元素中添加type=Button,您的问题就解决了。
Issue is solved, thx for solution
问题解决了,谢谢解决
优秀答案推荐
The problem is that your button refreshes the page onclick, which is a default behavior since you didn't specify the type attribute.
问题是您的按钮在单击时刷新页面,这是一个默认行为,因为您没有指定type属性。
When you don't specify the type attribute, it falls by default to type="submit" which refreshes the page upon click (after submitting the data; which is not what you're doing here).
我是一名优秀的程序员,十分优秀!