java - 如何处理ajax确认对话框是或否

标签 java ajax spring spring-mvc

在下面的代码中,当用户访问以下网址时,我尝试创建 ajax 调用,如下所示:

    "/product/remove/" + idx,

我想显示一个带有"is"和“否”的对话框,并且只有当用户输入前一个 url 并单击 Enter 时,并且在处理删除或删除操作的 Controller 逻辑执行之前,才会出现此对话框,这就是我在下面的 $.ajax 调用中使用“beforeSend”属性的原因。

我在谷歌上搜索了几篇关于如何使用 ajax 调用和 spring MVC 集成和创建确认对话框的帖子,但是我得到的大多数点击都需要进一步说明。

我想要实现的是,当用户单击"is"时,下面代码中显示的 Controller 应该正常执行。当用户单击“否”时,不会发生任何事情,只会出现对话框 应该消失。

请在下面找到我的尝试,并帮助我实现它

代码_1:

@<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Ajax confirm delete prodcut</title>
<script
src="${pageContext.request.contextPath }/resources/js/jquery-1.6.2.js"></script>

<script type="text/javascript">

$(document).ready(function() {


$('#confirmremoveform').click(function() {
var idx = $('#idx').val();
var ans = confirm("Are you sure you want to delete this Record?");
if (ans) {
    $.ajax({
        type: "DELETE",
        url: "/product/remove/" + idx,
        dataType : 'json',
        contentType : 'application/json',
        beforeSend: function () {
            $("#modal-book").modal("show");
          },
          success: function (data) {
            $("#modal-book .modal-content").html(data.html_form);
          },
        error: function (errormessage) {
            alert(errormessage.responseText);
        }
    });
}
});
}
</script>

</head>

<body>
<fieldset>
    <legend>confirmremove</legend>
    <input type="button" value="confirmremove" id="confirmremoveform" />
    <br/>
    <span id="result0"></span>
    </fieldset>
</body>

</html>

Controller

@Controller
@RequestMapping("/product/remove")
public class RemoveProductPageController {

public final static String sRemoveProductFromListAttributeName = "removeProductFromList";

public final static String CONTROLLER_URL = "/product/remove";
public final static String DO_REMOVE_HANDLER_METHOD_URL = CONTROLLER_URL + "/{idx}";

@Autowired
private ProductService productService;

@RequestMapping(value = "/{idx}", 
        method = RequestMethod.DELETE)
@ResponseBody
public ResponseEntity<String> doRemove(@Validated @Size(min = 0) @PathVariable(required = true) int idx,
        Model model) {

    Product productToBeRemove = productService.getProductFromListByIdx(idx);
    if (productToBeRemove == null) {
        return new ResponseEntity<String>("no product is avaialble at index:" + idx, HttpStatus.NOT_FOUND);
    }

    model.addAttribute(RemoveProductPageController.sRemoveProductFromListAttributeName, productToBeRemove);
    productService.removeProdcutFromListBxIdx(idx);
    return new ResponseEntity<String>("product removed from index: " + idx, HttpStatus.OK);
}
}

最佳答案

替换以下逻辑

var ans = confirm("Are you sure you want to delete this Record?");
if (ans) {
    //your all code
}

// with a single liner
if (confirm("Are you sure you want to delete this Record?")) {}

关于java - 如何处理ajax确认对话框是或否,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57968099/

相关文章:

java - Android:如何将位图图像传递到 ArrayList<Bitmap> 并检索它

javascript - 当我使用 .print() 函数时,AJAX 返回空页

java - 使用 Spring JPA 查询 Postgres jsonb 数据类型列

php - 单击产品图 block 从数据库获取产品数据

javascript - 如何使用 Ajax 和 Jquery 每 2 秒动态更改页面标题? (火狐浏览器无法运行?)

spring - 如何使用 Spring Data JPA + Spring Web MVC 避免 JSON 序列化中的延迟获取?

java - 重新启动 Java Spring Boot 应用程序

java - 与 ConditionalOnMissingBean 相反

java.sql.SQLException : Column count doesn't match value count at row 1 Servlets Error or Mysql Error?

Java 源文件夹 Eclipse