Kousik
Kousik

Reputation: 22415

Button disable after clicking issue

I have a submit button in my login page. I want to disable the submit button after clicking once.I'ts working fine with this.disabled = true; query and after disable the button it's going to the next page. The problem is that when I press back button it's still is disable because the page is not reloaded from server,It's loading from the local cache. I want to enable the button every time when I come back to the login page. I can clear the cache but there is some problem like if it will load from the server and project will be slow. What will be the best way to do this?

<script>
$(document).ready(function(){
    $(document).on('click','button',function(){
        this.disabled = true
        window.location="https://www.google.co.in/"
    })
})
</script>
<body>
<button type="submit" id="button">Click Me!</button> 
</body>

Upvotes: 5

Views: 4353

Answers (2)

SteveKB
SteveKB

Reputation: 190

I set the buttons initial disabled attribute to disabled in the body html to prove that the code does enable it again (so you can remove that in the body) I hope this helps:

<script>
$(document).ready(function () {
$("#button").removeAttr('disabled');
$("#button").click(function(){
        $(this).attr('disabled','disabled');
        window.location="https://www.google.co.in/"
    });
});
</script>
<body>
<button type="submit" id="button" disabled="disabled">Click Me!</button> 
</body>

Upvotes: 2

retrohacker
retrohacker

Reputation: 3224

You need another event that ensures the button is enabled on page load. Kind of like:

<script>
$(document).ready(function(){
    $("#button").disabled = false;
    $(document).on('click','button',function(){
        this.disabled = true
        window.location="https://www.google.co.in/"
    })
})
</script>
<body>
<button type="submit" id="button">Click Me!</button> 
</body>

I didn't try running this so I'm not sure if the syntax is 100% but it should give you the right idea.

Upvotes: 2

Related Questions