gpt4 book ai didi

javascript - 如何在运行时向 javascript 对象添加数据

转载 作者:行者123 更新时间:2023-11-28 15:14:57 25 4
gpt4 key购买 nike

我尝试以这种方式在运行时将数据添加到 js 对象,但出现错误。告诉我哪里出错了?

这一行给出了问题 Persons.push(new Person(id:id,name:name,salary:sal));

这是我的代码:

$(document).ready(function () {
$("#btnSave").click(function () {
var id,name,salary;
id=$("#txtID").val();
name=$("#txtName").val();
sal=$("#txtSal").val();
Persons.push(new Person(id:id,name:name,salary:sal));
return false;
});

$("#btnDelete").click(function () {
alert('delete');
return false;
});
});

var Persons = [];
var Person = function (id, name, salary) {
this.id = id;
this.name = name;
this.salary = salary;
};

这样我在js数组中添加编辑和删除数据。完整代码

<html>
<head><title>

</title>
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<style type="text/css">
.style1
{
width: 100%;
}
.style2
{
width: 197px;
}
</style>

<script type="text/javascript">
$(document).ready(function () {
$("#btnSave").click(function () {
var id, name, salary;
id = $("#txtID").val();
name = $("#txtName").val();
sal = $("#txtSal").val();
var index = IndexOfArrayByKeyValue(Persons, "id", id);
if (index == null) {
Persons.push(new Person(id, name, sal));
}
else {
Persons[index].id = id;
Persons[index].name = name;
Persons[index].salary = sal;
}
Show();
return false;
});

$("#btnDelete").click(function () {
var id, name, salary;
id = $("#txtID").val();
name = $("#txtName").val();
sal = $("#txtSal").val();
var index = IndexOfArrayByKeyValue(Persons, "id", id);
if (index != null) {
Persons.splice(index, 1);
}
Show();
return false;
});
});

var Persons = [];
var Person = function (id, name, salary) {
this.id = id;
this.name = name;
this.salary = salary;
};

function Show() {
var theTable = document.createElement('table');
for (var i = 0, tr, td; i < Persons.length; i++) {
tr = document.createElement('tr');
td = document.createElement('td');
td.appendChild(document.createTextNode(Persons[i].id + '\u00A0\u00A0\u00A0'));
tr.appendChild(td);

td = document.createElement('td');
td.appendChild(document.createTextNode(Persons[i].name + '\u00A0\u00A0\u00A0'));
tr.appendChild(td);

td = document.createElement('td');
td.appendChild(document.createTextNode(Persons[i].salary + '\u00A0\u00A0\u00A0'));
tr.appendChild(td);

theTable.appendChild(tr);
}
$("#Displaytable").html(theTable);
}

function findArrayByKeyValue(arraytosearch, key, valuetosearch) {
var Person = [];
for (var i = 0; i < arraytosearch.length; i++) {
if (arraytosearch[i][key] == valuetosearch) {
Person = arraytosearch[i];
}
}
return Person;
}

function IndexOfArrayByKeyValue(arraytosearch, key, valuetosearch) {
for (var i = 0; i < arraytosearch.length; i++) {
if (arraytosearch[i][key] == valuetosearch) {
return i;
}
}
return null;
}

</script>
</head>
<body>
<form method="post" action="WebForm1.aspx" id="form1">

<div>

<table class="style1">
<tr>
<td class="style2" colspan="2">
&nbsp;</td>
</tr>
<tr>
<td class="style2">
ID</td>
<td>
<input name="txtID" type="text" id="txtID" style="width:61px;" />
</td>
</tr>
<tr>
<td class="style2" colspan="2">
&nbsp;</td>
</tr>
<tr>
<td class="style2">
Name</td>
<td>
<input name="txtName" type="text" id="txtName" style="width:252px;" />
</td>
</tr>
<tr>
<td class="style2" colspan="2">
&nbsp;</td>
</tr>
<tr>
<td class="style2">
Salary</td>
<td>
<input name="txtSal" type="text" id="txtSal" />
</td>
</tr>
<tr>
<td class="style2" colspan="2">
&nbsp;</td>
</tr>
<tr>
<td class="style2">
<input type="submit" name="btnSave" value="Save" id="btnSave" />
&nbsp;
<input type="submit" name="btnDelete" value="Delete" id="btnDelete" />
</td>
<td>
&nbsp;</td>
</tr>
</table>
</div>
</form>
<br /><br /><br />
<div id='Displaytable'></div>
</body>
</html>

最佳答案

也许你应该更改这一行:

Persons.push(new Person(id:id,name:name,salary:sal));

对此:

Persons.push(new Person(id, 姓名, 工资));

关于javascript - 如何在运行时向 javascript 对象添加数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34464951/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com