1. var idUser = '';
    2. //tr下面索引大于0的所有子元素设置点击事件!
    3. $("tr:gt(0) .edit").click(function () {
    4. //find:获取后代为td的元素,eq(0):获取等于第0列的数据的html信息!
    5. idUser = $(this).parents("tr").find("td").eq(0).html();
    6. //跳转到修改页面,并且携带数据到修改页面
    7. window.location.href = "user/form?id=" + idUser;
    8. })
    9. //删除按钮的操作!
    10. $("tr:gt(0) .delete").click(function () {
    11. //find:获取后代为td的元素,eq(0):获取等于第0列的数据的html信息!
    12. idUser = $(this).parents("tr").find("td").eq(0).html();
    13. let b = confirm("确定删除吗?" + idUser);
    14. if (b) {
    15. //发送ajax请求--执行删除操作---返回处理结果---展示执行结果!
    16. deleteUser(idUser);
    17. } else {
    18. Toast.fire({
    19. icon: 'info',
    20. title: "取消了!",
    21. });
    22. }
    23. })
    24. function deleteUser(idUser) {
    25. // 准备一个对象!
    26. var admin = {
    27. id: idUser,
    28. };
    29. //将对象转为json串
    30. var str_json = JSON.stringify(admin);
    31. // 执行ajax请求:
    32. $.ajax({
    33. url: "user/delete", // 请求路径
    34. type: "POST", //请求方式
    35. //发送数据的类型!
    36. contentType: "application/json;charset=utf-8",
    37. dataType: "json",//设置接受到的响应数据的格式
    38. data: str_json,
    39. success: function (data) {
    40. console.log("执行删除成功!");
    41. Toast.fire({
    42. icon: 'success',
    43. title: data.message,
    44. });
    45. window.location.reload();
    46. },//响应成功后的回调函数
    47. error: function () {
    48. console.log("执行失败!");
    49. Toast.fire({
    50. icon: 'question',
    51. title: '执行删除失败了!!!!老弟'
    52. })
    53. return false;
    54. },//表示如果请求响应出现错误,会执行的回调函数
    55. });
    56. }