<body>

    <form action="javascript:void(0);" method="POST" onsubmit="app.Add()">
        <input type="text" id="add-name" placeholder="New country ">
        <input type="number" id="add-popu" placeholder="New population">
        <input type="submit" value="Add">
    </form>

    <div id="spoiler" role="aria-hidden">
        <form action="javascript:void(0);" method="POST" id="saveEdit">
            <input type="text" id="edit-name">
            <input type="text" id="edit-popu">
            <input type="submit" value="Ok" /> <a onclick="CloseInput()" aria-label="Close">&#10006;</a>
        </form>
    </div>

    <p id="counter"></p>

    <table>
      <tr>
        <th>Country</th>
        <th>Population</th>
      </tr>
        <tbody id="countries"></tbody>
        <tbody id="popultns"></tbody>
    </table>

    <script type="text/javascript" src="main.js"></script>

    <script>
    var app = new function()
    {   this.el = document.getElementById('countries');
        this.el = document.getElementById('popultns');
        let loadTableData = data2;
        this.Count = function(data)
        {   var el   = document.getElementById('counter');
            var name = 'country2';
            var pop = 'popu2'
            if (data)
            {   if (data > 1)
                {   name = 'countries' ;
                    pop = "popultns" ;
                }

                el.innerHTML = data + ' ' + name + ' ' + pop ;
            }
            else
                {   el.innerHTML = 'No ' + name + ' ' + pop  ;  }
        };

        this.FetchAll = function()
        {   var data = '';
            if (loadTableData.length > 0)
            {   for (i = 0; i < loadTableData.length; i++)
                {   data += '<tr>';
                    data += '<td>' + loadTableData[i].country + '</td>';
                    data += '<td>' + loadTableData[i].population + '</td>';
                    data += '<td><button onclick="app.Edit(' + i + ')">Edit</button></td>';
                    data += '<td><button onclick="app.Delete(' + i + ')">Delete</button></td>';
                    data += '</tr>';
                }
            }

            this.Count(loadTableData.length);
            return this.el.innerHTML = data;
        };

        this.Add = function ()
        {   el = document.getElementById('add-name');
            el2 = document.getElementById('add-popu');
            // Get the value
            var country2 = el.value;
            var pop = el2.value;

            if (country2)
            {   // Add the new value
                loadTableData.push(country2.trim() );

                // Reset input value
                el.value = '';
                //el2.value = '';
                // Dislay the new list
                this.FetchAll();
            }
        };




the image is the ui tht im working for the function
我应该基于可从我的main.js脚本(JSon格式)中获得的数据自动生成表。数据加载没有问题,因为数据可以显示在我的html文件中。但是,问题出在我单击“添加”按钮以更新表中的数据时,我的数据在自动生成的表上未定义返回时。当我单击编辑按钮时,数据将正确获取。只有在显示时,它才返回给我未定义的信息。

最佳答案

我知道怎么了。
在这里,我附上我完成的数据的代码和示例。
希望这会对像我这样在undrsntdng CRUD中遇到麻烦的应届毕业生有所帮助



<html>
	<head>
	  <meta charset="UTF-8">
	  <title>Countries CRUD</title>
	  <style>
		input[type='submit'], button, [aria-label]{
		  cursor: pointer;
		}
		#spoiler{
		  display: none;
		}
	  </style>
	</head>

	<body>

		<form action="javascript:void(0);" method="POST" onsubmit="app.Add()">
			<input type="text" id="add-name" placeholder="New country ">
			<input type="number" id="add-popu" placeholder="New population">
			<input type="submit" value="Add">
		</form>

		<div id="spoiler" role="aria-hidden">
			<form action="javascript:void(0);" method="POST" id="saveEdit">
				<input type="text" id="edit-name">
				<input type="text" id="edit-popu">
				<input type="submit" value="Ok" /> <a onclick="CloseInput()" aria-label="Close">&#10006;</a>
			</form>
		</div>

		<p id="counter"></p>

		<table>
		  <tr>
			<th>Country</th>
			<th>Population</th>
		  </tr>
			<tbody id="countries"></tbody>
			<tbody id="popultns"></tbody>
		</table>

		<script type="text/javascript" src="main.js"></script>

		<script>
		var app = new function()
		{	this.el = document.getElementById('countries' && 'popultns');
			//this.el = document.getElementById('popultns');
			let loadTableData = data2;
			this.Count = function(data)
			{	var el   = document.getElementById('counter');
				var name = 'country2';
				var pop = 'popu2'
				if (data)
				{	if (data > 1)
					{	name = 'countries' ;
						pop = "populations" ;
					}

					el.innerHTML = data + ' ' + name + ' ' + pop ;
				}
				else
					{	el.innerHTML = 'No ' + name + ' ' + pop  ;	}
			};

			this.FetchAll = function()
			{	var data = '';

				if (loadTableData.length > 0)
				{	for (i = 0; i < loadTableData.length; i++)
					{	data += '<tr>';
						data += '<td>' + loadTableData[i].country + '</td>';
						data += '<td>' + loadTableData[i].population + '</td>';
						data += '<td><button onclick="app.Edit(' + i + ')">Edit</button></td>';
						data += '<td><button onclick="app.Delete(' + i + ')">Delete</button></td>';
						data += '</tr>';
					}
				}

				this.Count(loadTableData.length);
				return this.el.innerHTML = data;
			};

			this.Add = function ()
			{	el = document.getElementById('add-name');
				el2 = document.getElementById('add-popu');


				// Get the value
				var country2 = el.value;
				var popltn = el2.value;

				if (country2 && popltn )
				{	// Add the new value
					//loadTableData.push(country2.trim() );
					loadTableData.push({country: country2, population:popltn});
					// Reset input value
					el.value = '';
					el2.value = '';
					// Dislay the new list
					this.FetchAll();
				}
			};

			this.Edit = function (item)
			{	var el3 = document.getElementById('edit-name');
				var el4 = document.getElementById('edit-popu');
				// Display value in the field
				let index = item;
				el3.value = loadTableData[index].country;
				el4.value = loadTableData[index].population;

				// Display fields
				document.getElementById('spoiler').style.display = 'block';
				self = this;
				document.getElementById('saveEdit').onsubmit = function()
				{
					// Get value
					var country2 = el3.value;
					var popltn = el4.value;

					//console.log({country2, pop});

					if (country2 || pop)
					//console.log(country2);
					{	// Edit value (strt,del,replace)
						console.log(country2,popltn);
						console.log(index, 1,({country2,popltn}));
						//update array
						loadTableData[index].country = el3.value;
						loadTableData[index].population = el4.value;
						//self.loadTableData.splice(index, 1,({country2,popltn}));
						//console.log(index, 1, pop.trim());
						//self.loadTableData.splice(index, 1, pop.trim());
						// Display the new list
						self.FetchAll();
						// Hide fields
						CloseInput();
					}
				}
			};

			this.Delete = function (item)
			{
			// Delete the current row
			loadTableData.splice(item, 1);
			// Display the new list
			this.FetchAll();
			};

		}

		app.FetchAll();
		function CloseInput()
		{	document.getElementById('spoiler').style.display = 'none';}

		</script>
	</body>
</html>

10-04 22:18
查看更多