0

I have a button, which when clicked I want to disable.

<input type="submit" value="Save" class="btn btn-default" name="submit" style="float: right; position: relative; right: -18px;" />

The value "Save" needs to pass from the button to the controller:

 public ActionResult EditCall(ModelCallDetails call, string submit)

but, obviously when I put an OnClick event such as disable, the parameter is not pushing to the controller.

Is there anyway of actually passing the value to the controller, the form is still posting the model info.

1
  • Dont use disabled, Use on click and once when you get parameters , dont do anything in onclick and return false Commented Apr 17, 2018 at 11:01

1 Answer 1

1

Use onsubmit event.

Example :

<form action="" method="post" onsubmit="return submitForm(this);">  
    <input type="submit" value="Save" class="btn btn-default" name="submit" style="float: right; position: relative; right: -18px;" /> 
</form>  

<script type="text/javascript">  
    function submitForm(th) {   
        th.submit.disabled = true;  
        return true;
    }  
</script>  

You can test in the below snippet that when the button becomes disable, the submit event isn't called anymore. I only changed return true to return false because I shouldn't post to stackoverflow. It's just to show you that the event isn't callable anymore after having its submit button setted to disabled=true.

<form action="" method="post" onsubmit="return submitForm(this);">  
        <input type="submit" value="Save" class="btn btn-default" name="submit" /> 
    </form>  

    <script type="text/javascript">  
        function submitForm(th) {   
            th.submit.disabled = true;
            console.log("passed");
            return false;
        }  
    </script> 

Sign up to request clarification or add additional context in comments.

4 Comments

This isn't stopping the user from clicking the button again though.
The submit button becomes disable and the form isn't posted again. Isn't it what you are looking for ?
unfortunately it posts the form several times if I continue clicking the button. @Sébastien S.
I added a snippet to my answer where you can see that the submit event isn't called anymore when the button is disabled.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.