我正在设置一个动态创建文本字段并将其附加到表格的表单。文本字段的名称是一个数组,因此它包含括号。下面的代码不附加输入字段,而是吐出[object HTMLInputElement]。我假设它是因为括号?有没有办法做到这一点?
//HTML
<table id="notesplus" cellpadding="0" cellspacing="0">
<thead></thead>
<tbody></tbody>
</table>
//JQUERY
$(document).ready(function() {
$('input[id=addIt]').live('click', function() {
addPlus();
});
});
function addPlus() {
var item = document.createElement("textarea");
item.setAttribute("name", "section[0][aplus]"+mycount+"[notes]");
var sRow1 = $('<tr>').appendTo('table#notesplus tbody');
var sCell1 = $('<td>').html(item).appendTo(sRow1);
}
答案 0 :(得分:0)
首先将您的选择器$('input[id=addIt]')
更改为$('#addIt')
。它快得多。
此外,我已经检查了您的代码,但它确实有效。唯一的问题是我添加了mycount
变量。
答案 1 :(得分:0)
目前您正在尝试使用html
插入HTML对象 - 但jQuery希望在此处收到HTML string,因此在将对象附加到您的单元格之前将其转换为文本。
将最后一行更改为:
var sCell1 = $('<td>').append(item).appendTo(sRow1);
答案 2 :(得分:0)
我会尝试这样做,看看是否有效。
function addPlus() {
var item = document.createElement("textarea");
var strName = "section[0][aplus]"+mycount+"[notes]";
item.setAttribute("name", strName);
$('table#notesplus tbody').append('<tr><td></td></tr>');
$('table#notesplus tbody tr:last-child td').append(item);
}
或类似的东西。