[英]How to turn an input text-value into an object's property value in Js?
我将输入文本值转换为inp1 var但是一旦我想在obj中调用它就不起作用了。
<input type="text" placeholder="Enter your text here" class="input fade-in" id="inp1" required>
var inp1 = document.getElementById("inp1").value;
$(function(){
$('#qrcode').qrcode({
width: 150,
height: 150,
text: "https://www.stackoverflow.com/" + inp1
});
});
我希望qrcode代码显示url +输入的文本
当页面加载时,您的代码运行一次 。 那时,输入字段仍然是空的。 相反,您可能希望在输入更改时更新qr代码。 你需要一个事件监听器:
$(function(){
var input = $("#inp1"); // if you use jQuery, use it everywhere. Also retrieve the element when the document loaded
input.on("change", function() { // listen for input changes
$('#qrcode').qrcode({ // then update the qr code
width: 150,
height: 150,
text: "https://www.stackoverflow.com/" + input.val(),
});
});
});
您可能需要考虑使用输入事件而不是更改事件,具体取决于您的用例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.