简体   繁体   English

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

[英]Getting the value of a specific input that is dynamically added from an ajax call

I want to get the value of a specific input on an onclick event我想在onclick事件上获取特定输入的值

This function retrieves all the data from my back-end这个 function 从我的后端检索所有数据

Javascript Javascript

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>

I tried using this我试过用这个

  $(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
  });

But console.logging this returns undefined on all button clicks, i tried hardcoding it但是console.logging这在所有按钮点击上都返回未定义,我尝试对其进行硬编码

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

And this returns a ID, the problem being it only ever returns the first ID all the time.这会返回一个 ID,问题是它始终只返回第一个 ID。

My question being, how do I get said input value from a onclick event?我的问题是,如何从 onclick 事件中获取所述输入值?

closest tests the element itself and its ancestors for matching the selector. closest测试元素本身及其ancestors是否匹配选择器。

What you need to do is: find closest tr to the button clicked and then inside of that tr find the input, so something like this:您需要做的是:找到最接近单击按钮的tr ,然后在该tr内部找到输入,如下所示:

  $(document).on('click', '.btn.btn-danger.deleteProject', function() {
    const id = $(this).closest('tr').find('.form-control.projectid').val();
    deleteProject(id);
  });

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

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