简体   繁体   English

如何从HTML表格数据填充JavaScript模型

[英]How to populate javascript model from html table data

I am having follwoing type of dynamic UI through table on my page 我的页面上的表格有以下动态UI类型

<table id="tblUserAttendance">
 <tr id="135"> <td> 1 </td>
               <td> Student 1 </td> 
               <td> <input checked="checked" name="rdo135" type="radio" value="true" /> Pre 
                    <input name="rdo135" type="radio" value="false" /> Absent
               </td>
 </tr>
<tr id="136"> <td> 1 </td>
               <td> Student 1 </td> 
               <td> <input checked="checked" name="rdo136" type="radio" value="true" /> Pre 
                    <input name="rdo136" type="radio" value="false" /> Absent
               </td>
 </tr>
<tr id="137"> <td> 1 </td>
               <td> Student 1 </td> 
               <td> <input checked="checked" name="rdo137" type="radio" value="true" /> Pre 
                    <input name="rdo137" type="radio" value="false" /> Absent
               </td>
 </tr>
</table>

I want to populate following type of model using above table dynamically to post data to server. 我想使用上表动态填充以下模型类型,以将数据发布到服务器。

var student = {};
  student.Id = Id;//where Id may be equal to 135
  studnet.IsPresent = true;// if that particualar radio button is checked  else false

i want to create array of such students which should be equal to the number of rows in the above table. 我想创建此类学生的数组,该数组应等于上表中的行数。

Can you please tell how to achieve this. 你能告诉我如何做到这一点。

This jQuery will create you an array of student objects: 此jQuery将为您创建一个学生对象数组:

<script type="text/ecmascript">
    var result = [];
    var students = $.each($('#tblUserAttendance tr'), function (i, e) {
        var studentData = $('td', e);
        var checks = $('input', studentData[2]);
        var student = { id: e.id, name: studentData[1].textContent, active: checks[0].checked };
        result[i] = student;
    });
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM