获取从ajax调用动态添加的特定输入的值

我想在onc​​lick事件上获取特定输入的值

此功能从我的后端检索所有数据

function getProjects() {
  $.ajax({
    method: 'GET',
    dataType: 'json',
    data: {
      functionToCall: 'project',
    },
    url: 'http://localhost/WBS/php/api/requests/get.php',
    success: (response) => {
      $.each(response, function () {
        $.each(this, function (index, value) {
          $('#project-body').append(
            `
            <tr>
                <td>
                  <input class="form-control projectid" type="hidden" name="projectid[]" value="${value.projectid}">
                </td>
                <td>
                  <input class="form-control projectName" type="text" name="projectName[]" value="${value.title}">
                </td>
                <td>
                  <input class="form-control description" type="text" name="description[]" value="${value.description}">
                </td>
                <td>
                  <input class="form-control estimatedTime" type="text" name="estimatedTime[]" value="${value.Estimated_time}">
                </td>
                <td>
                  <input class="form-control actualTime" type="text" name="actualTime[]" value="${value.Actual_time}">
                </td>
                <td>
                  <a class="btn btn-info addTask" href="Overview.html?id=${value.projectid}" role="button">
                    <i class="fa fa-angle-right" aria-hidden="true"> </i> Add task
                  </a>
                </td>
                <td>
                  <button value="${value.projectid}" type="button" name="deleteProject" class="btn btn-danger deleteProject">
                    <i class="fa fa-angle-right" aria-hidden="true"> </i> Delete project
                  </button>
                </td>
            </tr>

            `
          );
        });
      });
    },
    error: () => {
      console.error('Something went wrong with the getProjects function');
    },
  });
}

I would like to get the value of this specific input <td> <input class="form-control projectid" type="hidden" name="projectid[]" value="${value.projectid}"> </td>

我尝试使用这个

  $(document).on('click', '.btn.btn-danger.deleteProject', () => {
    let id = $(this).find('tr').closest('.form-control.projectid');
    deleteProject(id); // this is an ajax call that deletes a project with said ID
  });

但是console.logging这在所有按钮单击上返回未定义,我尝试对其进行硬编码

  $(document).on('click', '.btn.btn-danger.deleteProject', () => {
    let id = $('.btn.btn-danger.deleteProject').val();
    deleteProject(id);
  });

这将返回一个ID,问题是它始终一直仅返回第一个ID。

我的问题是,如何从onclick事件中获得输入值?

评论