从id获取tr的单元格值

时间:2018-05-10 07:04:21

标签: jquery

我正在使用AJAX,我无法从某一行的3个单元格中读取三个值。我需要第3,第4和第7个细胞

data: {
  status: "CLOSE",
  firstScore: $('#match' + i).find('td:eq(3)').text(),
  secondScore: $('#match' + i).find('td:eq(4)').text(),
  matchId: $('#match' + i).find('td:eq(7)').text()
},

这样的东西不起作用。每行都有ID #match + i 编辑: 我从服务器读取JSON,并尝试将其值放到表中,这一切都很成功。 以下是在trs中添加trs和tds的代码

$('#matchesForClose').append('<tr id="matchh' + i + '">');
var json = array[i];
$('#matchh' + i).append($("<td>" + json.date + "</td>"));
$('#matchh' + i).append($("<td>" + json.competition + "</td>"));
$('#matchh' + i).append($("<td>" + json.team1name + "</td>"));
$('#matchh' + i).append($("<td  contenteditable = true>" + json.score1 + "</td>"));
$('#matchh' + i).append($("<td  contenteditable = true>" + json.score2 + "</td>"));
$('#matchh' + i).append($("<td>" + json.team2name + "</td>"));
$('#matchh' + i).append($("<td >" + json.matchState + "</td>"));
$('#matchh' + i).append($("<td type='hidden'>" + json.matchId + "</td>"));
$('#matchh' + i).append($("<td >" + '<button class="btn btn-primary" id="submitClose' + i + '" value="Revert/Close">' + "</td>"));
$('#matchh' + i).append('</tr>');
$('#submitClose'+i).click(function(){
     $.ajax({
         url: 'closeMatch/confirm',
         data:
         {
             status: "CLOSE",
             firstScore: $('#matchh' + i).find('td:nth-of-type(3)').text(),
             secondScore: $('#matchh' + i).find('td:nth-of-type(4)').text(),
             matchId: $('#matchh' + i).find('td:nth-of-type(7)').text()
          },
          dataType: 'json',
          success: function (responseText){
              $("#closeMatches").modal('hide');
              alert(responseText);
          },
          error: function(responseText){
               alert("FATALITYYYY!");
          }
     })
});

关闭的匹配是简单的表

<div class="modal-body">
    <table class="table" id="matchesForClose">

    </table>
</div>

当我点击按钮时,会发送状态,但所有其他数据都为空: http://localhost:8081/closeMatch/confirm?status=CLOSE&firstScore=&secondScore=&matchId=

1 个答案:

答案 0 :(得分:0)

问题是您的i与您在内部点击事件中的完全不同。

尝试使用$(this).closest('.class')

&#13;
&#13;
// Don't put this function in loop. One binding for all buttons
$('.submit-button').click(function () {
  var table = $(this).closest('.modal-body').find('.table');
  
  alert(table.find('td').eq(1).text());
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="modal-body">
    <table class="table" id="matchesForClose1">
      <tr><td>T1.1</td><td>T1.2</td></tr>
    </table>
    <button id="submitForClose1" class="submit-button">Submit</button>
</div>
<div class="modal-body">
    <table class="table" id="matchesForClose2">
      <tr><td>T2.1</td><td>T2.2</td></tr>
    </table>
    <button id="submitForClose2" class="submit-button">Submit</button>
</div>
&#13;
&#13;
&#13;