Disable AddEventListener On Submit

前端 未结 3 1248
旧时难觅i
旧时难觅i 2021-01-21 11:27

I am trying to disable the function I pass to addEventListener when the user clicks on submit. I put code in to prevent user from leaving page if they have entered

3条回答
  •  悲&欢浪女
    2021-01-21 12:14

    Update

    To prompt the user before leaving a form:

    window.onbeforeunload = function(){
      return 'Are you sure you want to leave?';
    };
    

    See this post


    Use the required attribute on each field and you won't need to do all of that. The following demo will refuse any attempts to submit it's form if there's a blank field. It will send to a live test server and a response will be displayed verifying a successful submission.

    Demo

    window.onbeforeunload = function(){
      return 'Are you sure you want to leave?';
    };
    label {
      display: inline-block;
      width: 75px
    }
    
    [type=submit] {
      margin-left: 200px
    }



提交回复
热议问题