慕尼黑的夜晚无繁华
一种方法是创建一个array保存,我们称之为,object每次用户添加新记录时更新的航班。航班object包含两个属性:flightNumber和milesFlown。解决方案很简单,而不是每次提交新记录时重新绘制整个表,我们可以准备一个空的HTML,它将JavaScript根据用户条目(存储在航班objects 中array)填充。另外,查找重复条目的航班号,我们可以使用find(该方法Array在对象JavaScript),甚至没有穿过整个飞行循环object小号array(后来的功能会为我们做)。总而言之,这是一个有效的演示,它还包含一些有用的注释,可以在阅读代码时帮助您:/*** flightTable: the HTML table containing the flight records.* btn: the button that triggers a new record creation.* flightNum: the flight number field.* milesFlown: the miles flown field.* addRow: a function that adds a row to the HTML table based on the last record added.* flightArr: an array to store the flights objects.* flightNumVal: the flight number field value.* milesFlownVal: the miles flown field value.* i: curreent index of the flightArr table (used to print the last flight record to the HTML table for example).**/const flightTable = document.getElementById('flight-table'), btn = document.getElementById('display'), flightNum = document.getElementById('flight-number'), milesFlown = document.getElementById('miles-flown'), addRow = () => { /** I strongly suggest you continue reading then come back to this function after all the below **/ const tr = document.createElement('tr'), tdFN = document.createElement('td'), tdMF = document.createElement('td'); /** getting the last record in the flight objects array **/ tdFN.textContent = flightArr[i - 1].flightNumber; tdMF.textContent = flightArr[i - 1].milesFlown; /** append the TDs elements to the TR element (all of them are created above dynamically) **/ tr.append(tdFN, tdMF); /** append that row to the HTML table **/ flightTable.appendChild(tr); }let flightArr = [], flightNumVal = null, milesFlownVal = null, i = 0;btn.addEventListener('click', () => { /** CAUTION: I didn't make any checks to prevent non-numeric values for both fields **/ flightNumVal = flightNum.value; milesFlownVal = milesFlown.value; /** checking for duplicate entry **/ if (flightArr.find(el => { return el.flightNumber === flightNumVal })) { alert('Duplicate Flight Number entry: "' + flightNumVal + '"'); return false; } /** add the entry in the flight objects table **/ flightArr[i++] = { flightNumber: flightNumVal, milesFlown: milesFlownVal }; /** add the flight record to the array and increment the counter i (notice the i++) **/ addRow(); /** call addRow to add a new row in the table (HTML) **/});/** basic styling for demo purposes **/table { margin-top: 8px;}table, table tr th, table tr td { border: 1px solid #000; border-collapse: collapse;}table tr th { padding: 8px;}<label>Please enter your flight Number:</label><br><input type="text" id="flight-number" name="flightnumber" value="" /> <br /><label>Please enter Miles Flown:</label><br><input type="text" id="miles-flown" name="milesflown" value="" /><br><!-- no inline event handler on the input it will be attached in the "JavaScript" part --><input type="button" id="display" name="display" value="Submit Flight Information" /><!-- the table is prepared initially --><table id="flight-table"> <tr> <th>Flight Number</th> <th>Miles Flown</th> </tr></table>