submitting submit button even when canceling

3

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

Example on Fiddle

link

    $(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 do not want you to send it by clicking cancel .. or do not type.

    
asked by anonymous 23.03.2015 / 19:32

2 answers

2

You must join event.preventDefault() to prevent submitting form since this input has type="submit" .

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

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

Test 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: link

    
23.03.2015 / 19:38
2

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

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