简体   繁体   English

如何将 Object 值从 Javascript 文件传递到 HTML 文件

[英]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.

相关问题 如何将JSON对象从html传递到javascript文件 - how to pass JSON object from html to javascript file 如何将值从javascript传递到php文件 - How to pass value from javascript to php file 如何将输入框(html 文件)中的值传递给 javascript 注入文件(inject.js)? - How can I pass a value from an input box (html file) to a javascript inject file (inject.js)? 如何将一个 javascript 文件中的值传递到另一个 javascript 文件中 - how to pass a value from one javascript file into another javascript file 如何将变量从 html 文件传递​​到 javascript 文件 - how to pass a variable from html file to javascript file 如何将变量从 HTML 文件传递到 JavaScript 文件 - How to pass variable from an HTML file to a JavaScript file 如何将4个字符串值从HTML页面传递到javascript文件? - How pass 4 string values from HTML page to a javascript file? 如何将输入文件参数从 html 页面传递给 Javascript - How to pass input file parameter from html page to a Javascript 如何将元素值变量从 Javascript html 文件传递到 google apps 脚本中的 code.gs 文件? - How to pass an element-value variable from Javascript html file to code.gs file in google apps script? 如何在 javascript 中将 object 从一个文件传递到另一个文件? - How to pass object from one file to another in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM