从表格元素而不是整个表格序列化

问题描述:

尝试仅序列化特定表中的元素,但如果我执行整个Form

Trying to serialize just the elements from a specific table but it only returns a result if i do the whole Form

在下面的代码中,我只想对tbl2中的元素进行ajax

in the below code, i want to ajax just the elements in tbl2

<form>
 <input type="text" id="tb1" name="tbl1"/>
  <table name="tbl1">
   <tr><td><input type="text" name="tb2"/></td></tr>
 </table>
 <table name="tbl2">
   <tr><td><input type="text" name="tb3"/></td></tr>
   <tr><td><input type="text" name="tb4"/></td></tr>
 </table>
</form>

代码

var params = $("#tbl2").serialize();

var resp = $.ajax({
    async: false,
    type: "POST",
    url: AppRoot + "webhandlers/postback.ashx",
    data: params
});

首先,<table>不能具有name属性,即使有,

First and foremost, a <table> cannot have a name attribute, and even if it could, the jQuery ID selector (#) would not match it.

如果您改用id(<table id="tbl2">),它将像这样工作:

If you use id instead (<table id="tbl2">), it will work like this:

var params = $("#tbl2 :input").serialize();

:input选择器选择所有表单元素(在此处,在#tbl2内部),这是必需的,因为serialize()仅适用于那些表单元素.

The :input selector selects all the form elements (here, inside #tbl2), it is needed because serialize() will only work on those.

还请查看我的 jsFiddle演示.