简体   繁体   English

仅当用户选择特定单选按钮时,才显示确认/对话框并在该对话框中发送 POST 请求

[英]Show a confirmation/dialog and send POST request within that dialog box only if the user select specific radio button

I need to show a confirmation/dialog and send the POST request from within the dialogbox only if the user select a radiobutton among other radiobuttons with the value "waiting".只有当用户在其他单选按钮选择一个单选按钮时,我才需要显示一个确认/对话框并从对话框内发送 POST 请求。 In all other cases no confirmation/dialog needs to show up and the POST request needs to get fired directly from the #save element.在所有其他情况下,不需要显示确认/对话框,并且需要直接从#save元素触发 POST 请求。

Here is the form element in my JSP:这是我的 JSP 中的表单元素:

<form:form id="saveform" modelAttribute="form" action="#">
<div class="row form-group radio">
    <label class="col-xs-12 control-label text-right">
        <spring:message code="indicatorX"/> :
    </label>
    <div class="col-xs-12">
        <div class="has-error">
            <form:errors path="indicatorX" cssClass="help-block error" />
        </div>
        <label class="radio-inline radio-inline-espace">
            <form:radiobutton path="indicatorX" value="true" class="js-close" data-collapse="divX"/>
            <spring:message code="yes"/>
        </label>
        <label class="radio-inline radio-inline-espace">
            <form:radiobutton path="indicatorX" value="false" class="js-open" data-collapse="divX"/>
            <spring:message code="no"/>
        </label>
        <label class="radio-inline radio-inline-espace">
            <form:radiobutton path="indicatorX" value="waiting" class="js-close" data-collapse="divX"/>
            <spring:message code="Not responded"/>
        </label>
    </div>
</div>
<div class="row form-group radio">
    <label class="col-xs-12 control-label text-right">
        <spring:message code="indicatorY"/> :
    </label>
    <div class="col-xs-12">
        <div class="has-error">
            <form:errors path="indicatorX" cssClass="help-block error" />
        </div>
        <label class="radio-inline radio-inline-espace">
            <form:radiobutton path="indicatorY" value="true" class="js-close" data-collapse="divY"/>
            <spring:message code="yes"/>
        </label>
        <label class="radio-inline radio-inline-espace">
            <form:radiobutton path="indicatorY" value="false" class="js-open" data-collapse="divY"/>
            <spring:message code="no"/>
        </label>
        <label class="radio-inline radio-inline-espace">
            <form:radiobutton path="indicatorY" value="waiting" class="js-close" data-collapse="divY"/>
            <spring:message code="Not responded"/>
        </label>
    </div>
</div>
<a id="save" class="btn btn-primary" href="#modalConfirmation" data-toggle="modal" data-backdrop="static">
    <spring:message code="saveRecord" />
</a>
<!--  Modal element to confirm if user want to save -->
<div id="modalConfirmation" class="modal" tabindex="-1" role="dialog" data-backdrop="static" data-keyboard="false" data-show="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header"></div>
            <div class="modal-body">
                <spring:message code="MSG-XXX"/>
            </div>
            <div class="modal-footer">
                <button id="confirmYes" class="btn btn-default" data-dismiss="modal">
                    <spring:message code="no"/>
                </button>
                <button id="confirmYes" class="btn btn-primary" data-dismiss="modal">
                    <spring:message code="yes"/>
                </button>
            </div>
        </div>
    </div>
</div>

The POST endpoint in my controller我的控制器中的 POST 端点

    @RequestMapping(value = "/save", method = RequestMethod.POST)
public String save(@ModelAttribute FormObject form,
    BindingResult validation, RedirectAttributes redirectAttributes, ModelMap model) {
    LOG.info("webapp: method = save");

    //validation logic
    //service logic
    return ...;
}

Please bear in mind that I am taking over a project so haven't designed the architecture and currently I have to deal with some special cases as you can see the button is defined as an anchor element (with id save).请记住,我正在接管一个项目,所以还没有设计架构,目前我必须处理一些特殊情况,因为你可以看到按钮被定义为一个锚元素(带有 id 保存)。

The results achieved so far when trying several examples:到目前为止,在尝试几个示例时取得的结果:

A) The save button will either fire the POST request and show the dialog (while it should not)without waiting for user confirmation A) 保存按钮将触发 POST 请求并显示对话框(而它不应该),而无需等待用户确认

OR或者

B)If any of the radiobutton has the selected value "waiting" the request is fired but don't reach the server. B)如果任何单选按钮具有选定的值“等待”,则请求将被触发但不会到达服务器。

I had some difficulty to think how I could achieve that, I saw lot of examples on stack like this example dialogbox ajax on how to send from within a dialogbox but I think in my case I would need some validation logic before sending as an asynchronous call?我在思考如何实现这一点时遇到了一些困难,我在堆栈上看到了很多示例, 例如关于如何从对话框内发送的示例对话框 ajax ,但我认为在我的情况下,在作为异步调用发送之前,我需要一些验证逻辑? (AJAX) (阿贾克斯)

Here is my JS code with some simple logic to check if radiobutton has "waiting" value.这是我的 JS 代码,带有一些简单的逻辑来检查单选按钮是否具有“等待”值。

    var selectedValOne = $("[name='indicatorX']:checked").val();
    var selectedValTwo = $("[name='indicatorY']:checked").val();

    $('document').on(
      "click",
      "#save",
      function() {

        if (selectedValOne === 'waiting' || selectedValTwo === 'waiting') {
          $("#modalConfirmation").modal("show");

          $('#confirmYes').on('click', function() {
            showProgressbar();
            $("#saveform").attr(
              'action',
              PATH_CONTEXT +
              '/savingcontext/save');
            $("#saveform").submit();

          });

        } else {

          showProgressbar();
          $("#saveform").attr(
            'action',
            PATH_CONTEXT +
            '/savingcontext/save');
          $("#saveform").submit();

        }
      });

该按钮现在正在触发并调用模式并从 yes 按钮触发提交请求我不得不将 jquery 调用更改为 $('document').on( "click", "#save", function() {}) ;

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

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