如何将表 td 数据转换为特定行的 JSON?

托尼

嗨,我有动态数据,如果有的话,我正在生成 HTML 表格。现在我想分别提交每一行。但我无法从每一列中获取数据。请指导。这是我的表结构:

<table id="tblQuizzes" class="table table-striped">
<thead>
    <tr>

        <th>Student Name</th>
        <th>AGN Test 1</th>
        <th>Assignment</th>
        <th>Food Chef</th>
    </tr>
</thead>
<tbody>
    <tr id="tr_1">
        <td>
            Studet 1


        </td>
        <td>
            <input name="Id_1_2" value="29" form="form1" type="hidden">
            <input class="Id" style="width:30px;" value="9.00" name="1_2" form="form1">
            /10.00                                                <input name="OldValue_1_2" value="9.00" form="form1" type="hidden">



        </td>
        <td>
            <input name="Id_1_3" value="" form="form1" type="hidden">
            <input class="Id" style="width:30px;" value="" name="1_3" form="form1">
            /10.00                                                <input name="OldValue_1_3" value="" form="form1" type="hidden">



        </td>
        <td>
            <input name="Id_1_4" value="19" form="form1" type="hidden">
            <input class="Id" style="width:30px;" value="5.50" name="1_4" form="form1">
            /10.00                                                <input name="OldValue_1_4" value="5.50" form="form1" type="hidden">



        </td>
        <td><input class="btn btn-primary actions" form="form1" value="Save" type="submit"></td>
    </tr>
    <form method="POST" id="form1" action="Controller/Method"></form>


    <tr id="tr_2">
        <td>Student 2</td>
        <td>
            <input name="Id_2_2" value="" form="form2" type="hidden">
            <input class="Id" style="width:30px;" value="" name="2_2" form="form2">
            /50.00                                                <input name="OldValue_2_2" value="" form="form2" type="hidden">



        </td>
        <td>
            <input name="Id_2_3" value="22" form="form2" type="hidden">
            <input class="Id" style="width:30px;" value="17.00" name="2_3" form="form2">
            /50.00                                                <input name="OldValue_2_3" value="17.00" form="form2" type="hidden">



        </td>
        <td>
            <input name="Id_2_4" value="" form="form2" type="hidden">
            <input class="Id" style="width:30px;" value="" name="2_4" form="form2">
            /50.00                                                <input name="OldValue_2_4" value="" form="form2" type="hidden">



        </td>
        <td><input class="btn btn-primary actions" form="form2" value="Save" type="submit"></td>
    </tr>
    <form method="POST" id="form2" action="Controller/Method"></form>


    <tr id="tr_3">
        <td>
            Student 3


        </td>
        <td>
            <input name="Id_3_2" value="" form="form3" type="hidden">
            <input class="Id" style="width:30px;" value="" name="3_2" form="form3">
            /50.00                                                <input name="OldValue_3_2" value="" form="form3" type="hidden">



        </td>
        <td>
            <input name="Id_3_3" value="30" form="form3" type="hidden">
            <input class="Id" style="width:30px;" value="0.00" name="3_3" form="form3">
            /50.00                                                <input name="OldValue_3_3" value="0.00" form="form3" type="hidden">



        </td>
        <td>
            <input name="Id_3_4" value="" form="form3" type="hidden">
            <input class="Id" style="width:30px;" value="" name="3_4" form="form3">
            /50.00                                                <input name="OldValue_3_4" value="" form="form3" type="hidden">



        </td>
        <td><input class="btn btn-primary actions" form="form3" value="Save" type="submit"></td>
    </tr>
    <form method="POST" id="form3" action="Controller/Method"></form>




</tbody>

我想要这样的数据:

    [
{
id: 1,
newValue:"assignment",
oldValue:5,
},
{
id: 2,
newValue:"assignment",
oldValue:5,
},
{
id: 3,
newValue:"assignment",
oldValue:5,
}
]

这里的 HiddenField1 在 TD==Id,第二个输入文本框是 newValue,第三个 hiddenfild 是“oldValue”

马卡兰·帕蒂尔

我不太确定,但我认为你想要这样的东西。请参阅下面的代码片段。

请注意,我添加e.preventDefault();只是为了防止提交操作以在控制台中记录输出。您需要将其删除。

$(document).ready(function() {
  $('input[type="submit"]').click(function(e) {
    var output = [];
    $(this).parents('tr').find('td:gt(0):lt(3)').each(function() {
      var obj = {
        id: $(this).find('input')[0].value,
        newValue: $(this).find('input')[1].value,
        oldValue: $(this).find('input')[2].value
      };
      output.push(obj);
    });
    console.log(output);
    e.preventDefault();
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="tblQuizzes" class="table table-striped">
  <thead>
    <tr>
      <th>Student Name</th>
      <th>AGN Test 1</th>
      <th>Assignment</th>
      <th>Food Chef</th>
    </tr>
  </thead>
  <tbody>
    <tr id="tr_1">
      <td>Studet 1</td>
      <td>
        <input name="Id_1_2" value="29" form="form1" type="hidden">
        <input class="Id" style="width:30px;" value="9.00" name="1_2" form="form1"> /10.00
        <input name="OldValue_1_2" value="9.00" form="form1" type="hidden">
      </td>
      <td>
        <input name="Id_1_3" value="" form="form1" type="hidden">
        <input class="Id" style="width:30px;" value="" name="1_3" form="form1"> /10.00
        <input name="OldValue_1_3" value="" form="form1" type="hidden">
      </td>
      <td>
        <input name="Id_1_4" value="19" form="form1" type="hidden">
        <input class="Id" style="width:30px;" value="5.50" name="1_4" form="form1"> /10.00
        <input name="OldValue_1_4" value="5.50" form="form1" type="hidden">
      </td>
      <td><input class="btn btn-primary actions" form="form1" value="Save" type="submit"></td>
    </tr>
    <form method="POST" id="form1" action="Controller/Method"></form>
    <tr id="tr_2">
      <td>Student 2</td>
      <td>
        <input name="Id_2_2" value="30" form="form2" type="hidden">
        <input class="Id" style="width:30px;" value="" name="2_2" form="form2"> /50.00
        <input name="OldValue_2_2" value="" form="form2" type="hidden">
      </td>
      <td>
        <input name="Id_2_3" value="22" form="form2" type="hidden">
        <input class="Id" style="width:30px;" value="17.00" name="2_3" form="form2"> /50.00
        <input name="OldValue_2_3" value="17.00" form="form2" type="hidden">
      </td>
      <td>
        <input name="Id_2_4" value="" form="form2" type="hidden">
        <input class="Id" style="width:30px;" value="" name="2_4" form="form2"> /50.00
        <input name="OldValue_2_4" value="" form="form2" type="hidden">
      </td>
      <td><input class="btn btn-primary actions" form="form2" value="Save" type="submit"></td>
    </tr>
    <form method="POST" id="form2" action="Controller/Method"></form>
    <tr id="tr_3">
      <td>
        Student 3
      </td>
      <td>
        <input name="Id_3_2" value="31" form="form3" type="hidden">
        <input class="Id" style="width:30px;" value="" name="3_2" form="form3"> /50.00
        <input name="OldValue_3_2" value="" form="form3" type="hidden">
      </td>
      <td>
        <input name="Id_3_3" value="30" form="form3" type="hidden">
        <input class="Id" style="width:30px;" value="0.00" name="3_3" form="form3"> /50.00
        <input name="OldValue_3_3" value="0.00" form="form3" type="hidden">
      </td>
      <td>
        <input name="Id_3_4" value="" form="form3" type="hidden">
        <input class="Id" style="width:30px;" value="" name="3_4" form="form3"> /50.00
        <input name="OldValue_3_4" value="" form="form3" type="hidden">
      </td>
      <td><input class="btn btn-primary actions" form="form3" value="Save" type="submit"></td>
    </tr>
    <form method="POST" id="form3" action="Controller/Method"></form>
  </tbody>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章