我有一张这样的桌子
<table><tbody>
<tr class="row"><td style="display:none">902</td>
<td>visitas@greenblue.pe<input type="hidden" value="visitas@greenblue.pe" name="email[]"></td>
<td><input type="input" value="" class="validate" name="nombre[]"></td>
<td><input type="input" value="" name="empressa[]"></td>
<td><input type="input" value="" name="paginaWeb[]"></td>
<td><input type="input" value="" name="telefono[]"></td>
<td><input type="input" value="" name="cellular[]"></td>
</tr>
</tbody></table>
我试图使用Jquery创建一个数组,该数组将输入类型的名称作为数组的键。
var rows = [];
$('.row').each(function (i) {
var content = {};
$(this).find('td').each(function (j, v) {
var input = $("input", this),
name = input.attr("name").substring(0, input.attr("name").length - 2),
value = input.val();
content[name] = value;
});
rows.push(content);
});
但是,我说attr()
是未定义的,这是错误的。
有人能帮我吗
发布于 2015-05-26 02:19:43
由于每行中的第一个td
没有输入,所以input.attr("name")
将被取消定义,因此对.substring()
的调用将引发错误。
var rows = [];
$('.row').each(function (i) {
var content = {};
$(this).find('td input').each(function (j, v) {
var input = $(this),
name = input.attr("name").substring(0, input.attr("name").length - 2),
value = input.val();
content[name] = value;
});
rows.push(content);
});
你也可以
var rows = $('.row').map(function (i) {
var content = {};
$(this).find('td input').each(function (j, v) {
var name = this.name.replace(/\[\]$/, ''),
value = this.value;
content[name] = value;
});
return content;
}).get();
发布于 2015-05-26 02:19:41
首先,而不是
$(this).find('td').each(function(j, v) {
var input = $("input", this)
做
$(this).find('td input').each(function(j, v) {
var input = $(this)
var rows = [];
$('.row').each(function(i) {
var content = {};
$(this).find('td input').each(function(j, v) {
var input = $(this),
name = input.attr("name").substring(0, input.attr("name").length - 2),
value = input.val();
content[name] = value;
});
rows.push(content);
console.log(rows);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<table>
<tbody>
<tr class="row">
<td style="display:none">902</td>
<td>visitas@greenblue.pe
<input type="hidden" value="visitas@greenblue.pe" name="email[]">
</td>
<td>
<input type="input" value="" class="validate" name="nombre[]">
</td>
<td>
<input type="input" value="" name="empressa[]">
</td>
<td>
<input type="input" value="" name="paginaWeb[]">
</td>
<td>
<input type="input" value="" name="telefono[]">
</td>
<td>
<input type="input" value="" name="cellular[]">
</td>
</tr>
</tbody>
</table>
发布于 2015-05-26 02:41:41
第一件事是没有输入类型‘输入’。其次,对于没有输入子元素的第一个td,您将得到一个错误。这将导致异常和循环中断。
如果您使用type='text‘并在尝试使用其name属性之前检查输入的长度大于0,那么它将工作得很好。
这里有一个小提琴,它可以做你想做的事情:https://jsfiddle.net/vtaeht71/
var rows = [];
$('.row').each(function (i, row) {
var content = {};
$('td', row).each(
function (j, td) {
var input = $("input", td),
name, value;
if (input.length > 0) {
name = input.attr("name").substring(0, input.attr("name").length - 2),
value = input.val();
content[name] = value;
}
});
rows.push(content);
});
https://stackoverflow.com/questions/30455658
复制相似问题