gpt4 book ai didi

javascript - 数组推送改变格式jquery

转载 作者:行者123 更新时间:2023-11-28 00:12:30 25 4
gpt4 key购买 nike

var student = $('#student tr').find('td').find('input[type=\"text\"]');
student.each(function(index, element) {
var label = $(element).parent().find('span').text();
var member = $('#student').attr('name');
studentnames.push({
value: $(element).val(),
label: label,
member: member

});
});

这就是我制作数组的方式。这是输出

[{
"value": "jackie",
"label": "First Name",
"member": "science club"
}, {
"value": "chan",
"label": "Middle Name",
"member": "science club"
}, {
"value": "lee",
"label": "Last Name",
"member": "science club"
}, {
"value": "jr",
"label": "Suffix",
"member": "science club"
}, {
"value": "Tokyo",
"label": "City/Province",
"member": "science club"
}, {
"value": "Nanporo",
"label": "Town/Municipality",
"member": "science club"
}, {
"value": "Sorachi",
"label": "District",
"member": "science club"
}, {
"value": "1234",
"label": "Contact Number",
"member": "science club"
}]

如何将其更改为类似格式

[{
"First Name": "jackie",
"Middle Name": "chan",
"Last Name": "lee",
"Suffix": "jr",
"City/Province": "Tokyo",
"Town/Municipality": "Nanporo",
"District": "Sorachi",
"member": "science club"
}]

我的html表格是这样的

<table id="student" name="student" class="student">
<tr>
<td>
<center>
<input type="text" name="fname" class="fname" />
<br> <span><small><i>First Name</i></small></span> </center>
</td>
<td>
<center>
<input type="text" name="mname" class="mname" />
<br> <span><small><i>Middle Name</i></small></span> </center>
</td>
<td>
<center>
<input type="text" name="lname" class="lname" />
<br> <span><small><i>Last Name</i></small></span> </center>
</td>
</tr>
<tr>
<td>
<center>
<input type="text" name="suffix" class="suffix" />
<br> <span><small><i>Suffix</i></small></span> </center>
</td>
</tr>
<tr>
<td>
<center>
<input type="text" name="city" class="city" />
<br> <span><small><i>City/Province</i></small></span> </center>
</td>
<td>
<center>
<input type="text" name="town" class="town" />
<br> <span><small><i>Town/Municipality</i></small></span> </center>
</td>
<td>
<center>
<input type="text" name="district" class="district" />
<br> <span><small><i>District</i></small></span> </center>
</td>
</tr>
<tr>
<td>
<center>
<input type="text" name="contact" class="contact" />
<br> <span><small><i>Contact Number</i></small></span> </center>
</td>
</tr>
<tr>
<td>
<input type="button" id="addtable" value="Add" name="" /> </td>
<td>
<input type="button" id="removetable" value="Remove" name="" /> </td>
</tr>
</table>

添加和删除按钮有一个 jquery 函数,可以根据用户点击添加或删除相同的表

最佳答案

var studentnames = [];
$('#checkConsole').click(function() {
var student = $('#student tr').find('td').find('input[type=\"text\"]');

var temp = {};
student.each(function(index, element) {
var label = $(element).parent().find('span').text();
var member = $('#student').attr('name');

temp[label] = $(element).val();
temp['member'] = member;
});
studentnames.push(temp);
console.log(studentnames);
});

Jsfiddle

关于javascript - 数组推送改变格式jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30770604/

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