1

我有以下 HTMLTableRowElement。我需要根据输入和选择元素的名称字段和对应值将其转换为 json。是否有库或 jquery 方法可以做到这一点?当在行的 jquery 对象上调用时,jquery 的 serializeArray 方法返回一个空数组,例如 $('the following HTMLTableRowElement').serializeArray()。

<tr id="row7">
        <td>
            <input type="text" id="person_row7" name="person_row7">
        </td>
        <td>
            <input type="text" id="father_row7" name="father_row7">
        </td>
        <td>
            <input class="input-mini" type="text" id="age_row7" name="age_row7">
        </td>
        <td>
            <select id="gender_row7" class="input-small" name="gender_row7">
                <option value="" selected="selected">
                    ---------
                </option>
                <option>
                    Male
                </option>
                <option>
                    Female
                </option>
            </select>
        </td>
        <td>
            <input id="phonenumber_row7" name="phonenumber_row7" type="text">
        </td>
</tr>
4

1 回答 1

0

您需要针对 tr 内的表单元素 - serializeArray 不适用于 tr - 只有表单元素和表单控件

来自 jQuery .serializeArray()文档

此方法可以作用于已选择单个表单元素(例如 、 和 )的<input>jQuery<textarea>对象<select>。但是,通常更容易选择<form>标签本身进行序列化:

// serialize all form controls with a name attribute
$('#row7 :input[name]').serializeArray();

小提琴

于 2013-03-13T18:16:34.880 回答