Home > other >  Form not submitting when I try to disable submit button inside an onsubmit listener
Form not submitting when I try to disable submit button inside an onsubmit listener

Time:08-30

I have this problem where a form is not submitting after I have tampered with it's submit button a bit.

    $("#ID_mainform").on("submit",function(){
    $("button[name='example']").prop("disabled", true).attr("disabled", true);
})

The form submits all the inputs when this bit of code doesn't exist, but when I add this listener all it does is disables the button and refreshes the page like it submitted the form but in reality it just reloads the page.

So my question is why doesn't it submit the form? I have looked for answers everywhere but haven't yet found any.

CodePudding user response:

Kudos to @CBroe who led me in the right direction. The button I was trying to disable had a value and since it was inside a form the value got sent with the forms POST request. Disabling a button prevents it from getting sent with the POST request so what I did was this:

function loadSubmit() {
  $("button[name='example']").css({"pointer-events":"none", "background-color":"lightgray"})
}

I made a function and added it to the buttons onclick event. This mimics the disabling of the button without it actually disabling it.

  • Related