【问题标题】:how to handle yes or no of ajax confirmation dialog如何处理 ajax 确认对话框的是或否
【发布时间】:2020-01-17 22:55:13
【问题描述】:

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

    "/product/remove/" + idx,

我想显示一个带有“是”和“否”的对话框,并且该对话框应该仅在用户输入上一个 url 并单击 enter 并且在处理删除或删除操作的控制器的逻辑执行之前出现,这就是为什么我在下面的 $.ajax 调用中使用了“beforeSend”属性。

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

我想要实现的是,当用户单击是时,代码中显示的控制器应该正常执行。当用户单击否时,什么都不会发生,只有对话框 应该消失。

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

code_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
@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);
}
}

【问题讨论】:

    标签: java ajax spring spring-mvc


    【解决方案1】:

    替换以下逻辑

    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?")) {}
    

    【讨论】:

    • 谢谢..但我想在单击“是”的情况下..控制器继续正常执行,在单击“否”时,什么都没有发生,只有显示消失了..是这样吗
    • 有趣的是,您的意思是确认弹出窗口没有消失或删除?很奇怪,我现在没有得到你。
    • 嗨..你能看看这个问题吗:stackoverflow.com/questions/57974702/…几乎是同一个话题..我会删除这个帖子
    猜你喜欢
    • 2011-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多