从表格元素而不是整个表格序列化 [英] serialize from a table element and not the whole form

查看:88
本文介绍了从表格元素而不是整个表格序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试仅序列化特定表中的元素,但如果我执行整个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演示.

这篇关于从表格元素而不是整个表格序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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