简体   繁体   English

使用jQuery / JS获取表行值

[英]Get table row value with jQuery/JS

I am trying to get table value names with the following code: 我正在尝试使用以下代码获取表值名称:

my table : 我的桌子:

        function CreateBlockDeployTableRow(data) {
         return "<tr>" +
        "<td>" + data.EnvTypeName + "</td>" +
        "<td>" + data.AppTypeName + "</td>" +
        "<td>" + deleteBtn + "</td>" +
        "</tr>";
        }

my button: 我的按钮:

        var deleteBtn = '<button class="btn btn-danger unblock-button btn-sm 
        glyphicon glyphicon-minus" type=button > </button> '

on click function: 点击功能:

      function UnblockRequestHandler()
        {
        $('.unblock-button').click(function () {
        sendUnblockDeploySubmitHandler();
        });
       }

and here I want to send my Environment and Application to server side and take the value of the rows in the table , but the code is not working and not taking anything: 在这里,我想将我的环境和应用程序发送到服务器端,并获取表中的行的值,但是代码不起作用并且不接收任何内容:

       function sendUnblockDeploySubmitHandler() {
       var $row = jQuery(this).closest('tr');
       var $columns = $row.find('td');

        $columns.addClass('row-highlight');
        var values = "";

          jQuery.each($columns, function (i, item) {
           values = values + 'td' + (i + 1) + ':' + item.innerHTML + 
          '<br/>';
        alert(values);
         });
           console.log(values);
          }

html code : html代码:

       <div class="tbl-responsive">
      <table id="blockDeployTable" class="table table-hover 
       requestsblockDeployTable" style="font-size: 0.8em; margin-top: 120px; 
        text-align:center">
        <thead>
          <tr>
            <th>Environment</th>
            <th>Repo</th>
            <th>Actions</th>
          </tr>
        </thead>
        <tbody>
        </tbody>
      </table>
    </div>

Where am I wrong here I cannot understand? 我在哪里错在这里我听不懂?

You are trying to access jQuery(this) in the sendUnblockDeploySubmitHandler() method but the this handle does not refer to the button element. 您尝试在sendUnblockDeploySubmitHandler()方法中访问jQuery(this) ,但是this句柄未引用button元素。 I would suggest you to directly pass the callback function when you bind the click event. 我建议您在绑定click事件时直接传递回调函数。

function UnblockRequestHandler() {
    $('.unblock-button').click(sendUnblockDeploySubmitHandler);
}

This way, you can access the corresponding button element using jQuery(this) in the sendUnblockDeploySubmitHandler() 这样,您可以在sendUnblockDeploySubmitHandler()使用jQuery(this)访问相应的button元素。

You could just pass the click event through to the sendUnblockDeploySubmitHandler function: 您可以将click事件传递给sendUnblockDeploySubmitHandler函数:

$('.unblock-button').click(function () {
  sendUnblockDeploySubmitHandler(event); // pass event to function
});

function sendUnblockDeploySubmitHandler(event) {
  var $row = $(event.target).closest('tr'); // event.target is the button
  var $columns = $row.find('td');

  $columns.addClass('row-highlight');
  var values = "";

  jQuery.each($columns, function (i, item) {
   values = values + 'td' + (i + 1) + ':' + item.innerHTML + '<br/>';
   alert(values);
  });

  console.log(values);
}

I've put together a jsbin to show this working: https://jsbin.com/sosimudubi/1/edit?html,js,console,output 我整理了一个jsbin来显示此工作: https ://jsbin.com/sosimudubi/1/edit?html,js,console,output

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

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