[英]How to Pass Object Value From A Javascript File to HTML File
I created an object with an array of products and I want to retrieve its value in a label in my html file but I don't know what's the right format.我用一系列产品创建了一个 object,我想在我的 html 文件中的 label 中检索它的值,但我不知道什么是正确的格式。 My javascript and html file are in separated files.我的 javascript 和 html 文件位于单独的文件中。 Can someone help me with this.有人可以帮我弄这个吗。 Thanks a lot!非常感谢!
const database = [ { name: "test", brand: "test", description: "test desc", price: "100", }, { name: "test2", brand: "test brand", description: "test desc2", price: "200", }, { name: "test3", brand: "test brand", " description": "test desc3", price: "300", }, ]; function addToCart() { return console.log(database[0].name) }
<div class="card"> <img src= "img/1.jpg" alt="Avatar"> <div class="container"> <span> <input type="text" placeholder="Enter items" id="productName" value=$database[0].name /> <button onclick="addToCart()" id="addToCart">Buy</button> </div> </div>
const database = [ { name: "test", brand: "test", description: "test desc", price: "100", }, { name: "test2", brand: "test brand", description: "test desc2", price: "200", }, { name: "test3", brand: "test brand", " description": "test desc3", price: "300", }, ]; function addToCart() { document.getElementById('productName').value = database[0].name; }
<div class="card"> <img src= "img/1.jpg" alt="Avatar"> <div class="container"> <span> <input type="text" placeholder="Enter items" id="productName"/> <button onclick="addToCart()" id="addToCart">Buy</button> </div> </div>
try this.尝试这个。 it will work它会起作用的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.