簡體   English   中英

如何將 Object 值從 Javascript 文件傳遞到 HTML 文件

[英]How to Pass Object Value From A Javascript File to HTML File

我用一系列產品創建了一個 object,我想在我的 html 文件中的 label 中檢索它的值,但我不知道什么是正確的格式。 我的 javascript 和 html 文件位於單獨的文件中。 有人可以幫我弄這個嗎。 非常感謝!

 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>

嘗試這個。 它會起作用的

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM