首页 > 解决方案 > 如何在jquery中获取动态表数据中文本框的值

问题描述

我创建了一个带有文本输入框的表格。我想在 tr 元素中获取文本框值的值。我该怎么做?

$(document).on('click', '.overtime_send', function(){

    $('#employee_table tr').each(function(row, tr){

      var asd = $(tr).find('td:eq(3)').text();//get the value
      var asd01 = $(tr).find('td:eq(4)').text();//not get the value of the text box
        alert(asd);
  });    

  });

上面的代码获取表行的所有静态内容。如何将文本值放入表数据中的变量中?

标签: javascriptjquery

解决方案


使用 the ,.find('input')因为您的trand中只有一个输入.val()来获取值。

let your_array = [];

$(document).on('click', '.overtime_send', function() {

  $('#employee_table tbody tr').each(function(row, tr) {

    your_array.push({
      emp_id: $(tr).find('td:eq(0)').text(),
      input_val: $(tr).find('input').val()
    });

  });

});

推荐阅读