jquery - 如何将带有名称数组的输入附加到一行? [英] jquery - how to append input with name array to a row?

查看:13
本文介绍了jquery - 如何将带有名称数组的输入附加到一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设置一个表单,该表单将动态创建一个文本字段并将其附加到表格中.文本字段的名称是一个数组,因此它包含括号.下面的代码没有附加输入字段,而是返回 [object HTMLInputElement].我假设这是因为括号?有没有办法做到这一点?

I'm setting up a form that will dynamically create a text field and append it to the table. The name of the text field is an array, so it contains brackets. The code below doesn't append the input field, rather, it spits back [object HTMLInputElement]. I'm assuming it's because of the brackets? Is there a way to do this?

//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);
}

推荐答案

目前您正在尝试使用 html 插入 HTML 对象 - 但 jQuery 期望收到一个 HTML 字符串 在这里,因此在将对象附加到单元格之前将其转换为文本.

At the moment you are trying to using html to insert an HTML object - but jQuery expects to receive an HTML string here and so converts your object to text before appending it to your cell.

将最后一行改为:

var sCell1 = $('<td>').append(item).appendTo(sRow1);

这篇关于jquery - 如何将带有名称数组的输入附加到一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆