Submit button even when cancel

Asked

Viewed 680 times

3

I have a button in ASP.NET webForm and even clicking cancel it sends the Submit.

Example in Fiddle

http://jsfiddle.net/dorathoto/qywvuhvq/

    $(function () {
        $("[name='chk_IDDel']").change(function () {
            var disabled = true;
            $("[name='chk_IDDel']").each(function () {
                if ($(this).is(':checked')) {
                    disabled = false;
                    if ($("input[name=chk_IDDel]:checked").length > 1) {
                        document.getElementById("<%= btn_Deletar.ClientID%>").value = 'Deletar imóveis';
                 }
                 else {
                     document.getElementById("<%= btn_Deletar.ClientID%>").value = 'Deletar imóvel'
                 }
             }
         });
         $('#<%= btn_Deletar.ClientID%>').prop("disabled", disabled);
     });

     $("#<%= btn_Deletar.ClientID%>").click(function () {
         var confirma = prompt("digite a palavra 'confirmar' para deletar", "");
         if (confirma.toUpperCase() == "CONFIRMAR") {
             document.all.submit();
         }
         else {
             return false;
         }
     });
 });

What am I doing wrong in JQUERY? I don’t want you to send when clicking cancel.. or not typing.

2 answers

2

At no time does your code prevent the submission of the form, to do this you must call the method preventDefault() of the event passed as a parameter of the function created in .click()

$("#<%= btn_Deletar.ClientID%>").click(function (e) {
    e.preventDefault();
    // continuação do seu código
}

2


You have to join event.preventDefault() to prevent form Submit once that input has type="submit".

I suggest you add this and also fix the code in case the "cancel" is clicked and with this the variable confirms not to receive any value giving error Uncaught TypeError: Cannot read property 'toUpperCase' of null because it cannot call the method the toUpperCase() in a variable that has value null (and not string).

I suggest you still change document.all.submit(); for document.querySelector('form').submit(); or if you have an ID you use the ID. You can also use $(this).closest('form').submit(); which has the advantage of being specific to that <form>.

Forehead like this:

$("#btn_Deletar").click(function (e) {
    e.preventDefault();
    var confirma = prompt("digite a palavra 'confirmar' para deletar", "") || '';
    if (confirma.toUpperCase() == "CONFIRMAR") {
        document.querySelector('form').submit();

jsFiddle: http://jsfiddle.net/o1oaju47/1

  • for some reason it is working, but has stopped the functionality of ASP:Button (not running the code)

  • I removed e.preventDefault(); that problem in Webform and it worked http://jsfiddle.net/dorathoto/qywvuhvq/1/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.