嵌套对象未使用 javascript 插入到表中

我是 JavaScript 新手

在此代码中,我尝试companyDetails(nested object of productDetails)按其 id 添加表。
就像,对于id1,我想在id输入 id 1 的输入字段中添加 companyDetails 并在给定的输入字段中插入详细信息,
所以我的问题是companyDetails(date,address,companyName)在为输入的 id 添加其值后不会显示在表中

<!DOCTYPE html>

<html>


<head>

    <script src="add.js"></script>

    <style>

        th,

        td,

        p,

        input {

            font-family: Arial, Helvetica, sans-serif;

        }


        table,

        th,

        td {

            border: solid 1px #DDD;

            border-collapse: collapse;

            padding: 10px 10px;

            text-align: center;

        }


        th {

            font-weight: bold;

        }

    </style>

</head>


<body onload="display()">

    <h2>Product Details:</h2>

    <form action="">

        <label for="id">Id: </label>&nbsp;&nbsp; <input type="number" id="productId" required> <input type="button"

            value="autofill" onclick="auto()"><br><br> 


        <label for="EstablishDate">Establish Date:</label>&nbsp;&nbsp; <input type="date" id="date" required><br><br>

        <label for="address">Address:</label><br><br> <textarea name="address" id="address" cols="30"

            rows="10"></textarea><br><br>

        <label for="CompanyName">Company Name:</label>&nbsp;&nbsp; <input type="text" id="companyName" required><br><br>



        <input type="button" value="add company details" onclick="addCompanyDetails()"><br><br>

        <p id="result"></p>

        <p id="demo"></p>

        <p id="messageTableA"></p>

    </form>

</body>


</html>


蝴蝶刀刀
浏览 160回答 3
3回答

梵蒂冈之花

在这里,我将具有特定 ID 的公司信息添加到其各自的产品 id 中,例如productDetails[i].address = Caddress;在 addCompanyDetails()功能中,然后在我已替换的显示功能中companyDetails.address to data[i].address。 但请确保当您重新加载页面时它会丢失数据。let productDetails = [&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; id: "1",&nbsp; &nbsp; &nbsp; &nbsp; partNo: "10",&nbsp; &nbsp; &nbsp; &nbsp; productName: "bag",&nbsp; &nbsp; &nbsp; &nbsp; size: "30",&nbsp; &nbsp; &nbsp; &nbsp; color: ["Blue"],&nbsp; &nbsp; &nbsp; &nbsp; description: "sky bags ",&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; id: "2",&nbsp; &nbsp; &nbsp; &nbsp; partNo: "15",&nbsp; &nbsp; &nbsp; &nbsp; productName: "bottle",&nbsp; &nbsp; &nbsp; &nbsp; size: "10",&nbsp; &nbsp; &nbsp; &nbsp; color: ["Green", "Orange"],&nbsp; &nbsp; &nbsp; &nbsp; description: "plastic and still",&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; id: "4",&nbsp; &nbsp; &nbsp; &nbsp; partNo: "20",&nbsp; &nbsp; &nbsp; &nbsp; productName: "lunchbox",&nbsp; &nbsp; &nbsp; &nbsp; size: "20",&nbsp; &nbsp; &nbsp; &nbsp; color: ["Blue", "Red"],&nbsp; &nbsp; &nbsp; &nbsp; description: "fresh food",&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; id: "3",&nbsp; &nbsp; &nbsp; &nbsp; partNo: "40",&nbsp; &nbsp; &nbsp; &nbsp; productName: "pen",&nbsp; &nbsp; &nbsp; &nbsp; size: "10",&nbsp; &nbsp; &nbsp; &nbsp; color: ["Red", "Blue"],&nbsp; &nbsp; &nbsp; &nbsp; description: "gel pen ",&nbsp; &nbsp; }, {&nbsp; &nbsp; &nbsp; &nbsp; id: "5",&nbsp; &nbsp; &nbsp; &nbsp; partNo: "35",&nbsp; &nbsp; &nbsp; &nbsp; productName: "notebook",&nbsp; &nbsp; &nbsp; &nbsp; size: "30",&nbsp; &nbsp; &nbsp; &nbsp; color: ["Blue", "Red", "Orange"],&nbsp; &nbsp; &nbsp; &nbsp; description: "Writing",&nbsp; &nbsp; }]/** * function to add company details */function addCompanyDetails() {&nbsp; &nbsp; let data = (document.getElementById('productId').value);&nbsp; &nbsp; let date1 = document.getElementById('date').value;&nbsp; &nbsp; let Caddress = document.getElementById('address').value;&nbsp; &nbsp; let Cname = (document.getElementById('companyName').value);&nbsp; &nbsp; if (data === '') {&nbsp; &nbsp; &nbsp; &nbsp; message("enter id for search");&nbsp; &nbsp; }&nbsp; &nbsp; for (let i = 0; i < productDetails.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; let companyDetails = productDetails[i].companyDetails ? productDetails[i].companyDetails : { date: "", address: "", companyName: "" };&nbsp; &nbsp; &nbsp; &nbsp; let p = companyDetails;&nbsp; &nbsp; &nbsp; &nbsp; if ((productDetails[i].id) == (data)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; p.companyName = Cname ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; productDetails[i].date = date1 ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; productDetails[i].address = Caddress;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; productDetails[i].companyName=Cname;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; displayData();&nbsp; &nbsp; &nbsp; &nbsp; clearInputData();&nbsp; &nbsp; }}/** * this function display the data in table */function displayData(companyDetails) {&nbsp; &nbsp; objectArray = Object.values(productDetails);&nbsp; &nbsp; display(objectArray, companyDetails,clearInputData);}/** * this function is for get the value from form */function getValue() {&nbsp; &nbsp; let id = document.getElementById('productId').value;&nbsp; &nbsp; let date = document.getElementById('date').value;&nbsp; &nbsp; let address = document.getElementById('address').value;&nbsp; &nbsp; let companyName = document.getElementById('companyName').value;&nbsp; &nbsp; return { id, date, address, companyName };}/** * Function is to display the data in table */function display(productStore,callBack) {&nbsp; &nbsp; messageTable(" ");&nbsp; &nbsp; let data = productDetails;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; let table = "<table border = 1 cellpadding = 10 ><th colspan=7 >Product Details</th><th colspan=7 >company Details</th><tr><th>Product Id</th><th>Part No</th><th>Name</th><th>Size</th><th>Color</th><th>Description</th><th>weight</th><th>Date</th><th>Address</th><th>Company name</th></tr>";&nbsp; &nbsp; for (let i = 0; i < data.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if (data[i].productWeight === undefined) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data[i].productWeight = " ";&nbsp; &nbsp; &nbsp; &nbsp; } else { }&nbsp; &nbsp; &nbsp; &nbsp; if (data[i].companyName === undefined) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data[i].companyName = " ";&nbsp; &nbsp; &nbsp; &nbsp; } else { }&nbsp; &nbsp; &nbsp; &nbsp; if (data[i].date === undefined) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data[i].date = "&nbsp; &nbsp;";&nbsp; &nbsp; &nbsp; &nbsp; } else { }&nbsp; &nbsp; &nbsp; &nbsp; if (data[i].address === undefined) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data[i].address = "&nbsp; &nbsp;";&nbsp; &nbsp; &nbsp; &nbsp; } else { }&nbsp; &nbsp; &nbsp; &nbsp; table += "<tr><td>" + data[i].id + "</td>";&nbsp; &nbsp; &nbsp; &nbsp; table += "<td>" + data[i].partNo + "</td>";&nbsp; &nbsp; &nbsp; &nbsp; table += "<td>" + data[i].productName + "</td>";&nbsp; &nbsp; &nbsp; &nbsp; table += "<td>" + data[i].size + "</td>";&nbsp; &nbsp; &nbsp; &nbsp; table += "<td>" + data[i].color + "</td>";&nbsp; &nbsp; &nbsp; &nbsp; table += "<td>" + data[i].description + "</td>";&nbsp; &nbsp; &nbsp; &nbsp; table += "<td>" + data[i].productWeight + "</td>";&nbsp; &nbsp; &nbsp; &nbsp; table += "<td>" + data[i].date + "</td>";&nbsp; &nbsp; &nbsp; &nbsp; table += "<td>" + data[i].address + "</td>";&nbsp; &nbsp; &nbsp; &nbsp; table += "<td>" + data[i].companyName + "</td>";&nbsp; &nbsp; }&nbsp; &nbsp; messageTable(table);&nbsp; &nbsp; clearInputData();}/** * function is to print the table */function messageTable(data) {&nbsp; &nbsp; document.getElementById("messageTableA").innerHTML = data;}/** * this function is to clear the data */function clearInputData() {&nbsp; &nbsp; document.getElementById("productId").value = "";&nbsp; &nbsp; document.getElementById("address").value = "";&nbsp; &nbsp; document.getElementById("date").value = "";&nbsp; &nbsp; document.getElementById("companyName").value = "";}<!DOCTYPE html><html><head>&nbsp; &nbsp; <script src="home.js"></script>&nbsp; &nbsp; <style>&nbsp; &nbsp; &nbsp; &nbsp; th,&nbsp; &nbsp; &nbsp; &nbsp; td,&nbsp; &nbsp; &nbsp; &nbsp; p,&nbsp; &nbsp; &nbsp; &nbsp; input {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; font-family: Arial, Helvetica, sans-serif;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; table,&nbsp; &nbsp; &nbsp; &nbsp; th,&nbsp; &nbsp; &nbsp; &nbsp; td {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; border: solid 1px #DDD;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; border-collapse: collapse;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; padding: 10px 10px;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text-align: center;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; th {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; font-weight: bold;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; </style></head><body onload="display()">&nbsp; &nbsp; <h2>Product Details:</h2>&nbsp; &nbsp; <form action="">&nbsp; &nbsp; &nbsp; &nbsp; <label for="id">Id: </label>&nbsp;&nbsp; <input type="number" id="productId" required> <input type="button"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value="autofill" onclick="auto()"><br><br>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; <label for="EstablishDate">Establish Date:</label>&nbsp;&nbsp; <input type="date" id="date" required><br><br>&nbsp; &nbsp; &nbsp; &nbsp; <label for="address">Address:</label><br><br> <textarea name="address" id="address" cols="30"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rows="10"></textarea><br><br>&nbsp; &nbsp; &nbsp; &nbsp; <label for="CompanyName">Company Name:</label>&nbsp;&nbsp; <input type="text" id="companyName" required><br><br>&nbsp; &nbsp; &nbsp; &nbsp; <input type="button" value="add company details" onclick="addCompanyDetails()"><br><br>&nbsp; &nbsp; &nbsp; &nbsp; <p id="result"></p>&nbsp; &nbsp; &nbsp; &nbsp; <p id="demo"></p>&nbsp; &nbsp; &nbsp; &nbsp; <p id="messageTableA"></p>&nbsp; &nbsp; </form></body></html>

幕布斯6054654

getObjById接受一个对象数组和您要查找的 id,并返回给定数组中具有您请求的 id 的对象//if the id requested does not exist in the array you gave, it returns undefinedfunction getObjById(arr,id){return arr.filter(a=>a.id==id)[0]}//example usage(valid/existing id)console.log(getObjById(productDetails,"1"))//example usage(invalid/non-existing id)console.log(getObjById(productDetails,"99"))//because I have == and not === 1=="1"console.log(getObjById(productDetails,1))<script>//just where I have the array to save line space for the functionlet productDetails = [&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; id: "1",&nbsp; &nbsp; &nbsp; &nbsp; partNo: "10",&nbsp; &nbsp; &nbsp; &nbsp; productName: "bag",&nbsp; &nbsp; &nbsp; &nbsp; size: "30",&nbsp; &nbsp; &nbsp; &nbsp; color: ["Blue"],&nbsp; &nbsp; &nbsp; &nbsp; description: "sky bags ",&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; id: "2",&nbsp; &nbsp; &nbsp; &nbsp; partNo: "15",&nbsp; &nbsp; &nbsp; &nbsp; productName: "bottle",&nbsp; &nbsp; &nbsp; &nbsp; size: "10",&nbsp; &nbsp; &nbsp; &nbsp; color: ["Green", "Orange"],&nbsp; &nbsp; &nbsp; &nbsp; description: "plastic and still",&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; id: "4",&nbsp; &nbsp; &nbsp; &nbsp; partNo: "20",&nbsp; &nbsp; &nbsp; &nbsp; productName: "lunchbox",&nbsp; &nbsp; &nbsp; &nbsp; size: "20",&nbsp; &nbsp; &nbsp; &nbsp; color: ["Blue", "Red"],&nbsp; &nbsp; &nbsp; &nbsp; description: "fresh food",&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; id: "3",&nbsp; &nbsp; &nbsp; &nbsp; partNo: "40",&nbsp; &nbsp; &nbsp; &nbsp; productName: "pen",&nbsp; &nbsp; &nbsp; &nbsp; size: "10",&nbsp; &nbsp; &nbsp; &nbsp; color: ["Red", "Blue"],&nbsp; &nbsp; &nbsp; &nbsp; description: "gel pen ",&nbsp; &nbsp; }, {&nbsp; &nbsp; &nbsp; &nbsp; id: "5",&nbsp; &nbsp; &nbsp; &nbsp; partNo: "35",&nbsp; &nbsp; &nbsp; &nbsp; productName: "notebook",&nbsp; &nbsp; &nbsp; &nbsp; size: "30",&nbsp; &nbsp; &nbsp; &nbsp; color: ["Blue", "Red", "Orange"],&nbsp; &nbsp; &nbsp; &nbsp; description: "Writing",&nbsp; &nbsp; }]</script>

MMMHUHU

let productDetails = [&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; id: "1",&nbsp; &nbsp; &nbsp; &nbsp; partNo: "10",&nbsp; &nbsp; &nbsp; &nbsp; productName: "bag",&nbsp; &nbsp; &nbsp; &nbsp; size: "30",&nbsp; &nbsp; &nbsp; &nbsp; color: ["Blue"],&nbsp; &nbsp; &nbsp; &nbsp; description: "sky bags ",&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; id: "2",&nbsp; &nbsp; &nbsp; &nbsp; partNo: "15",&nbsp; &nbsp; &nbsp; &nbsp; productName: "bottle",&nbsp; &nbsp; &nbsp; &nbsp; size: "10",&nbsp; &nbsp; &nbsp; &nbsp; color: ["Green", "Orange"],&nbsp; &nbsp; &nbsp; &nbsp; description: "plastic and still",&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; id: "4",&nbsp; &nbsp; &nbsp; &nbsp; partNo: "20",&nbsp; &nbsp; &nbsp; &nbsp; productName: "lunchbox",&nbsp; &nbsp; &nbsp; &nbsp; size: "20",&nbsp; &nbsp; &nbsp; &nbsp; color: ["Blue", "Red"],&nbsp; &nbsp; &nbsp; &nbsp; description: "fresh food",&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; id: "3",&nbsp; &nbsp; &nbsp; &nbsp; partNo: "40",&nbsp; &nbsp; &nbsp; &nbsp; productName: "pen",&nbsp; &nbsp; &nbsp; &nbsp; size: "10",&nbsp; &nbsp; &nbsp; &nbsp; color: ["Red", "Blue"],&nbsp; &nbsp; &nbsp; &nbsp; description: "gel pen ",&nbsp; &nbsp; }, {&nbsp; &nbsp; &nbsp; &nbsp; id: "5",&nbsp; &nbsp; &nbsp; &nbsp; partNo: "35",&nbsp; &nbsp; &nbsp; &nbsp; productName: "notebook",&nbsp; &nbsp; &nbsp; &nbsp; size: "30",&nbsp; &nbsp; &nbsp; &nbsp; color: ["Blue", "Red", "Orange"],&nbsp; &nbsp; &nbsp; &nbsp; description: "Writing",&nbsp; &nbsp; }]/** * function to add company details */function addCompanyDetails() {&nbsp; &nbsp; let data = (document.getElementById('productId').value);&nbsp; &nbsp; let Name = (document.getElementById('companyName').value);&nbsp; &nbsp; let Cdate = (document.getElementById('date').value);&nbsp; &nbsp; let Caddress = (document.getElementById('address').value);&nbsp; &nbsp; if (data === '') {&nbsp; &nbsp; &nbsp; &nbsp; message("enter id for search");&nbsp; &nbsp; }&nbsp; &nbsp; for (let i = 0; i < productDetails.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if ((productDetails[i].id) == (data)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; productDetails[i].companyDetails = {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; date: "",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; companyName: "",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; address: ""&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; productDetails[i].companyDetails.companyName = Name;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; productDetails[i].companyDetails.date = Cdate;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; productDetails[i].companyDetails.address = Caddress;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(productDetails[i].companyDetails.companyName);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; displayData();&nbsp; &nbsp; &nbsp; &nbsp; clearInputData();&nbsp; &nbsp; }}/** * this function display the data in table */function displayData() {&nbsp; &nbsp; objectArray = Object.values(productDetails);&nbsp; &nbsp; display(objectArray, clearInputData);}/** * this function is for get the value from form */function getValue() {&nbsp; &nbsp; let id = document.getElementById('productId').value;&nbsp; &nbsp; let date = document.getElementById('date').value;&nbsp; &nbsp; let address = document.getElementById('address').value;&nbsp; &nbsp; let companyName = document.getElementById('companyName').value;&nbsp; &nbsp; return { id, date, address, companyName };}/** * Function is to display the data in table */function display(productStore, callBack) {&nbsp; &nbsp; messageTable(" ");&nbsp; &nbsp; let data = productDetails;&nbsp; &nbsp; let table = "<table border = 1 cellpadding = 10 ><th colspan=7 >Product Details</th><th colspan=7 >company Details</th><tr><th>Product Id</th><th>Part No</th><th>Name</th><th>Size</th><th>Color</th><th>Description</th><th>weight</th><th>Date</th><th>Address</th><th>Company name</th></tr>";&nbsp; &nbsp; for (let i = 0; i < data.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; if (data[i].productWeight === undefined) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data[i].productWeight = " ";&nbsp; &nbsp; &nbsp; &nbsp; } else { }&nbsp; &nbsp; &nbsp; &nbsp; if (data[i].date === undefined) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data[i].date = "&nbsp; &nbsp;";&nbsp; &nbsp; &nbsp; &nbsp; } else { }&nbsp; &nbsp; &nbsp; &nbsp; if (data[i].address === undefined) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data[i].address = "&nbsp; &nbsp;";&nbsp; &nbsp; &nbsp; &nbsp; } else { }&nbsp; &nbsp; &nbsp; &nbsp; let companyDetails = data[i].companyDetails ? data[i].companyDetails : { date: "", address: "", companyName: "" };&nbsp; &nbsp; &nbsp; &nbsp; table += "<tr><td>" + data[i].id + "</td>";&nbsp; &nbsp; &nbsp; &nbsp; table += "<td>" + data[i].partNo + "</td>";&nbsp; &nbsp; &nbsp; &nbsp; table += "<td>" + data[i].productName + "</td>";&nbsp; &nbsp; &nbsp; &nbsp; table += "<td>" + data[i].size + "</td>";&nbsp; &nbsp; &nbsp; &nbsp; table += "<td>" + data[i].color + "</td>";&nbsp; &nbsp; &nbsp; &nbsp; table += "<td>" + data[i].description + "</td>";&nbsp; &nbsp; &nbsp; &nbsp; table += "<td>" + data[i].productWeight + "</td>";&nbsp; &nbsp; &nbsp; &nbsp; table += "<td>" + companyDetails.date + "</td>";&nbsp; &nbsp; &nbsp; &nbsp; table += "<td>" + companyDetails.address + "</td>";&nbsp; &nbsp; &nbsp; &nbsp; table += "<td>" + companyDetails.companyName + "</td>";&nbsp; &nbsp; }&nbsp; &nbsp; messageTable(table);&nbsp; &nbsp; clearInputData();}/** * function is to print the table */function messageTable(data) {&nbsp; &nbsp; document.getElementById("messageTableA").innerHTML = data;}/** * this function is to clear the data */function clearInputData() {&nbsp; &nbsp; document.getElementById("productId").value = "";&nbsp; &nbsp; document.getElementById("address").value = "";&nbsp; &nbsp; document.getElementById("date").value = "";&nbsp; &nbsp; document.getElementById("companyName").value = "";}<!DOCTYPE html><html><head>&nbsp; &nbsp; <script src="add.js"></script>&nbsp; &nbsp; <style>&nbsp; &nbsp; &nbsp; &nbsp; th,&nbsp; &nbsp; &nbsp; &nbsp; td,&nbsp; &nbsp; &nbsp; &nbsp; p,&nbsp; &nbsp; &nbsp; &nbsp; input {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; font-family: Arial, Helvetica, sans-serif;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; table,&nbsp; &nbsp; &nbsp; &nbsp; th,&nbsp; &nbsp; &nbsp; &nbsp; td {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; border: solid 1px #DDD;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; border-collapse: collapse;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; padding: 10px 10px;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text-align: center;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; th {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; font-weight: bold;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; </style></head><body onload="display()">&nbsp; &nbsp; <h2>Product Details:</h2>&nbsp; &nbsp; <form action="">&nbsp; &nbsp; &nbsp; &nbsp; <label for="id">Id: </label>&nbsp;&nbsp; <input type="number" id="productId" required> <input type="button"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value="autofill" onclick="auto()"><br><br>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; <label for="EstablishDate">Establish Date:</label>&nbsp;&nbsp; <input type="date" id="date" required><br><br>&nbsp; &nbsp; &nbsp; &nbsp; <label for="address">Address:</label><br><br> <textarea name="address" id="address" cols="30"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rows="10"></textarea><br><br>&nbsp; &nbsp; &nbsp; &nbsp; <label for="CompanyName">Company Name:</label>&nbsp;&nbsp; <input type="text" id="companyName" required><br><br>&nbsp; &nbsp; &nbsp; &nbsp; <input type="button" value="add company details" onclick="addCompanyDetails()"><br><br>&nbsp; &nbsp; &nbsp; &nbsp; <p id="result"></p>&nbsp; &nbsp; &nbsp; &nbsp; <p id="demo"></p>&nbsp; &nbsp; &nbsp; &nbsp; <p id="messageTableA"></p>&nbsp; &nbsp; </form></body></html>现在它完全可以工作,无需删除嵌套对象
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript