Skip to content
Advertisement

Submit button not consistently firing

I have an asp.net core razor form with a submit button. When the user clicks ‘Update’, the form calls a JavaScript function to prompt the user if they are sure they want to complete the request. This functionality has been working, but starting last week, the form will occasionally not save despite the user confirming that they want to complete the request.

<div class="form-group">
    <label asp-for="Requests.Complete" class="lblReq"></label>
    <select id="isComplete" asp-for="Requests.Complete" class="inputReq switch-disable">
        <option value="N">N</option>
        <option value="Y">Y</option>
    </select>
        <span asp-validation-for="Requests.Complete" class="text-danger"></span>
</div> 

   
<div class="form-group">
    <input type="submit" value="Update" class="btn btn-primary" onclick="return CompleteOrNot()" />
</div>


@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}

<script type="text/javascript">
    $(document).ready(function () {
        
    function CompleteOrNot() {
        var value = ($("#isComplete").val());            
        if (value == "Y")
        {
            var retVal = confirm("Are you sure you want to complete this request?");
            window.history.go(-1);                
            if (retVal == true) {                    
                return true;
            }                
        }                   
    }
}

The user always gets prompted if they want to complete the request and sometimes the post goes through and other times it acts as though the user said no to the prompt (even though they did not). Do you have any suggestions on how to troubleshoot/correct this issue?

Advertisement

Answer

If you want to submit the form when retVal is true,try to use the following code

<form method="post" id="myForm">
    <div class="form-group">
    <label asp-for="Requests.Complete" class="lblReq"></label>
    <select id="isComplete" asp-for="Requests.Complete" class="inputReq switch-disable">
        <option value="N">N</option>
        <option value="Y">Y</option>
    </select>
        <span asp-validation-for="Requests.Complete" class="text-danger"></span>
</div> 


    <div class="form-group">
        <input type="button" value="Update" class="btn btn-primary" onclick="CompleteOrNot()" />
    </div>
</form>
@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}

    <script type="text/javascript">
        function CompleteOrNot() {
            var value = ($("#isComplete").val());
            if (value == "Y") {
                var retVal = confirm("Are you sure you want to complete this request?");
                if (retVal == true) {
                    $("#myForm").submit();
                }
            }
        }
        $(document).ready(function () {

            
        })

    </script>
}
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement