2013-03-13 54 views
1

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

<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> 

回答

0

您需要针对TR内的表单元素 - serializeArray不会在TR工作 - 仅构成元素和表单控件

从jQuery的.serializeArray()文档

这种方法可以作用于已选择各个表单元素的jQuery对象,如<input>,<textarea><select>。然而,通常更容易选择<form>标签本身序列化:

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

FIDDLE