gpt4 book ai didi

javascript - Jquery AJAX DELETE 请求也在同一表单上触发 POST 请求

转载 作者:行者123 更新时间:2023-12-02 20:57:44 25 4
gpt4 key购买 nike

我有一个表格包裹在 table 上

                <form id="project-form">
<table id="project-table" class="table table-striped table-inverse table-responsive">
<caption>Projects</caption>
<thead class="thead-inverse">
<tr>
<th scope="col">#</th>
<th scope="col">Project name</th>
<th scope="col">Description</th>
<th scope="col">Estimated time (min)</th>
<th scope="col">Actual time (min)</th>
<th scope="col">Add task</th>
<th scope="col">Delete project</th>
</tr>
</thead>
<tbody id="project-body">


</tbody>
</table>
</form>

此表通过 AJAX GET 请求填充

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

我想在此按钮点击时调用删除函数

               <button type="submit" id="deleteProject" name="deleteProject" class="btn btn-danger" >
<i class="fa fa-angle-right" aria-hidden="true"> </i> Delete project
</button>

我在所述按钮上调用的函数是这个

function deleteProject() {
let id = $(this).find('#projectid').val();

$.ajax({
type: 'DELETE',
url: 'http://localhost/WBS/php/api/requests/delete.php',
data: { id: id },
success: () => {
$('#alertDanger').fadeIn(() => {
setTimeout(() => {
$('#alertDanger').fadeOut();
}, 5000);
});
},
error: () => {
$('#alertDangerFailed').fadeIn(() => {
setTimeout(() => {
$('#alertDangerFailed').fadeOut();
}, 5000);
});
},
});
}

在 document.ready 中我处理 onclick 事件

  $('#deleteProject').on('click', () => {
deleteProject();
});

现在,当我单击deleteproject按钮时,问题就出现了,因为我的表单还可以上传新项目,所以deleteproject提交事件也会触发在此onclick事件下绑定(bind)的POST调用

  $('#saveProjects').on('click', () => {
uploadProjects();
});

编辑:使用此修复它


$(document).on('click', '#deleteProject', () => {
deleteProject();
});

作为来自 -> Jquery button click event not firing 的 onclick 处理程序

最佳答案

Kohaku回答,这是 <button type="submit"></button 的默认行为所以你必须以某种方式阻止它。一种可能的解决方案是将按钮类型从 submit 更改为至button ,应该是这样的:

<button type="button" id="deleteProject" name="deleteProject" class="btn btn danger">
<i class="fa fa-angle-right" aria-hidden="true"> </i> Delete project
</button>

关于javascript - Jquery AJAX DELETE 请求也在同一表单上触发 POST 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61432032/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com