如何创建密钥=>;Javascript中集合对象中的值数组
我已创建此HTML表:如何创建密钥=>;Javascript中集合对象中的值数组,javascript,jquery,Javascript,Jquery,我已创建此HTML表: <table class="table table-condensed"> <thead> <tr> <th><input type="checkbox" id="toggleCheckboxSelFabricante" name="toggleCheckboxSelFabricante"></th> <th>Fabr
<table class="table table-condensed">
<thead>
<tr>
<th><input type="checkbox" id="toggleCheckboxSelFabricante" name="toggleCheckboxSelFabricante"></th>
<th>Fabricante</th>
</tr>
</thead>
<tbody id="selFabricanteBody">
<tr>
<td><input type="checkbox" name="selChkFabricante" id="selChkFabricante3" value="3"></td>
<td>Eos est ipsam.</td>
</tr>
</tbody>
</table>
换句话说,我需要的是每个manufacturerColl
拥有和id
=>名称,例如:
manufacturerColl[] = {
id: someId,
name: someName
};
我不确定,但也许这能奏效:
// move foreach selected checkbox and get the needed
for (var j = 0; j < checkedManufacturers.length; j++) {
manufacturerColl.push({
id: checkedManufacturers[j].value,
name: "" // how do I get the value on the second column?
});
}
//移动每个选中的复选框并获取所需的
对于(变量j=0;j
哪种方法是正确的?如何在每次迭代中获得第二列的值
方法
我不知道这是否是完全正确的,但这是我所做的,它的马车。参见代码:
var checkedModelBranch = $("#parMarcaModeloFabricanteBody").find("input[type='checkbox']:checked"),
checkedManufacturers = $("#selFabricanteBody").find("input[type='checkbox']:checked"),
// I added this var to get all the names
checkedManufacturersName = $("#selFabricanteBody").find("input[type='checkbox']:checked").parent().next('td').text(),
manufacturerColl = [],
modelBranchManufacturerCollection;
for (var j = 0; j < checkedManufacturers.length; j++) {
manufacturerColl.push({
id: checkedManufacturers[j].value,
// Here I'm tying to put the entire name but get only the first character
name: checkedManufacturersName[j]
});
}
for (var i = 0; i < checkedModelBranch.length; i++) {
modelBranchManufacturerCollection = addNewRelationModelBranchManufacturer(checkedModelBranch[i].value, manufacturerColl);
if (modelBranchManufacturerCollection) {
//$("#parMarcaModeloFabricanteBody td#" + checkedModelBranch[i].value).siblings().attr("rowspan", modelBranchManufacturerCollection.manufacturerKeyCollection.length);
for (var k = 0; k < modelBranchManufacturerCollection.manufacturerKeyCollection.length; k++) {
// then I render the name attribute from the collection
$("#parMarcaModeloFabricanteBody td#" + checkedModelBranch[i].value).append((modelBranchManufacturerCollection.manufacturerKeyCollection)[k].name + '<br/>');
}
}
}
var checked = $('#selFabricanteBody').find('input[type="checkbox"]:checked');
var arr = [], j = checked.length, item;
while(j--) {
item = list[j];
//gets the text from the next child, assumes its the text
//if the text is before checkbox, use previousSibling
arr.push({id: item.value, name: item.parentNode.nextSibling.nodeValue});
}
var checkedModelBranch=$(“#parmarcamododelofabricantebody”).find(“输入[type='checkbox']:选中”),
checkedManufacturers=$(“#selFabricanteBody”).find(“输入[type='checkbox']:选中”),
//我添加了这个变量来获取所有的名称
checkedManufacturersName=$(“#selFabricanteBody”).find(“输入[type='checkbox']:选中”).parent().next('td').text(),
制造商成本=[],
模型BranchManufactureCollection;
对于(变量j=0;j ');
}
}
}
为什么我只插入/获取第一个字符而不是完整的字符串?//因为您的输入和文本被包装在td元素中
在守则中:
var checkedModelBranch = $("#parMarcaModeloFabricanteBody").find("input[type='checkbox']:checked"),
checkedManufacturers = $("#selFabricanteBody").find("input[type='checkbox']:checked"),
// I added this var to get all the names
checkedManufacturersName = $("#selFabricanteBody").find("input[type='checkbox']:checked").parent().next('td').text(),
manufacturerColl = [],
modelBranchManufacturerCollection;
for (var j = 0; j < checkedManufacturers.length; j++) {
manufacturerColl.push({
id: checkedManufacturers[j].value,
// Here I'm tying to put the entire name but get only the first character
name: checkedManufacturersName[j]
});
}
for (var i = 0; i < checkedModelBranch.length; i++) {
modelBranchManufacturerCollection = addNewRelationModelBranchManufacturer(checkedModelBranch[i].value, manufacturerColl);
if (modelBranchManufacturerCollection) {
//$("#parMarcaModeloFabricanteBody td#" + checkedModelBranch[i].value).siblings().attr("rowspan", modelBranchManufacturerCollection.manufacturerKeyCollection.length);
for (var k = 0; k < modelBranchManufacturerCollection.manufacturerKeyCollection.length; k++) {
// then I render the name attribute from the collection
$("#parMarcaModeloFabricanteBody td#" + checkedModelBranch[i].value).append((modelBranchManufacturerCollection.manufacturerKeyCollection)[k].name + '<br/>');
}
}
}
var checked = $('#selFabricanteBody').find('input[type="checkbox"]:checked');
var arr = [], j = checked.length, item;
while(j--) {
item = list[j];
//gets the text from the next child, assumes its the text
//if the text is before checkbox, use previousSibling
arr.push({id: item.value, name: item.parentNode.nextSibling.nodeValue});
}
这是我用来创建一个对象的,用于发送到一个控制器方法,该控制器方法正在寻找一个类/模型作为参数:
function ExtractModel(array) {
var modelString = '';
var model = {};
//var array = arrayString.split('&');
var isObjectModel = $.isPlainObject(array);
if (!isObjectModel) {
$(array).each(function (index, element) {
var typeId = element.split('=')[0];
var val = element.split('=')[1];
if (element == null) { } else {
model[typeId] = (val);
}
});
} else {
model = array;
}
return model;
})
因此,通过这个,我获取一个url参数字符串并从中创建一个对象;参数字符串是通过序列化表单生成的。“model”自动创建由“typeId”表示的属性;如果“typeId”=“hello”,则将创建一个名为“hello”的新属性或对象项(model.hello将为您获取一个值)。您可以使用相同的逻辑来循环页面上的元素,以使用多个元素填充对象。确保将变量“modelBranchManufacturerCollection”设置为{},然后设置插入新变量的位置索引
这应该满足您的要求:
var ids = [1, 2, 3, 4, 5, 6]
var names = ['a', 'b', 'c', 'd', 'e', 'f']
var mainObj = {};
$(ids).each(function (index, element) {
var miniObj = {};
miniObj['id'] = ids[index];
miniObj['name'] = names[index];
mainObj[index] = miniObj;
});
返回6个id和名称项。@apsillers我在主帖子中添加了一些额外的信息,看看这是否清楚了问题和我需要的内容。如果你指的是HTML中复选框旁边的文本,那只是文本。没有办法轻易得到它。您可以使用id将其包装在一个span中,或者使用硬编码的映射,如so['text1','text2']
,其中数组索引是复选框值(稀疏数组是javascript能够很好地处理的为数不多的事情之一)当您反复检查复选框时,只需从该数组中提取“name”值。@JaredSmith,然后按照您的建议进行操作,我该如何获取?很抱歉,我在主帖中输入了一个拼写错误,因此在进行此操作之前,没有显示所有已包装的表,你能看一下我的最新编辑吗?我很难相信方法链接会在这里起作用。你真的应该像我的回答中那样使用循环。如果需要,可以使用$().find()
来获取初始列表,但要迭代它并获取node.nextSibling.nodeValue
以推入checkedManufacturerName
数组。复选框来自这里checkedManufacturers=$(“#selFabricanteBody”).find(“输入[type='checkbox']:checked”)
我该如何在你的代码中加入这一点,并且有点迷失你很好。更新了我的答案