Disable button after insert

Asked

Viewed 48 times

1

I want the button, after clicking to insert, to be locked. I can only insert once each button, which represents a task.

Knob:

<button class="btn btn-info" onclick="return confirm('Pretende registar esta atividade?');this.disabled=true;">Pequeno Almoço</button>

I’m trying with this.disabled=true; but after clicking and entering in the database, it is in the same asset.

script on the page:

function myFunction() {
var today = new Date();
var date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
var dateTime = date+' '+time;
document.getElementById("IniciarTarefa1").value = dateTime;
}

(function ($) {$(document).ready(function(e) {
$("#form,#form1").submit(function(e){ 
	$.ajax({
        type: "POST",
        url: "./conexaoteste",
        data: $("#form,#form1").serialize(), // serializes the form's elements.
        dataType: "json",
            success: function (data){						
			}, 
            error: function(data){
                $(".error_message").removeClass('hide'); // error message
            },
	});

});
});

  • When Voce speaks that it inserts into the database, how does Voce do it? Voce redirects?

  • @Localhost do via post with ajax

  • So instead of making $("#form,#Form1"). Submit(Function(e)); put in a function! you don’t need Ubmit, and on the button call this function or add an event to the button.

2 answers

2

Good evening, remove Return at start:

<button class="btn btn-info" onclick="confirm('Pretende registar esta atividade?');this.disabled=true;">Pequeno Almoço</button>

Ha if you want to save a return from Alert you can do so:

<script>
     var a = null;
</script>
<button class="btn btn-info" onclick="a = confirm('Pretende registar esta atividade?');this.disabled=true;">Pequeno Almoço</button>
<button class="btn btn-info" onclick="alert(a);">Ver variável</button>
  • by removing the return the problem arises that when the alert of the confirm If clicking cancel disables the button, and I don’t mean it, I only want you to disable it if you enter this activity once. Another problem when clicking disables the button, but when disabling no longer inserts in the database

  • 1

    So put this.disabled = confirm('Do you want to register this activity?'); it will only deactivate if you click yes!

  • that’s just what I needed. Thank you

1


Use an if and remove the ; between the if and the this.disabled:

<button class="btn btn-info" onclick="if(confirm('Pretende registar esta atividade?')) this.disabled=true;">Pequeno Almoço</button>

This will create a condition on confirm where the if will only be accessed if you click on OK.

  • with the condition solved my problem, but when testing with another button I detected a problem. If you click on two tasks and do Ok inserts and disables the button, but if on the third task click the button and do cancelar re-activates the two previous buttons and should not.

  • So you’re doing it differently, because the this refers to the button clicked.

  • What’s happening is that when I do cancelar refreshes the page. I have the button as put above

  • But if you refresh the page the buttons go back to normal. There’s nothing wrong with that.

  • yes, I know. And since I don’t want you to refresh the page while recording tasks, I never update the page until all tasks have been recorded. But what’s happening is, if you click ok never refreshes the page, but if you click cancelar refresh the page, how can I do not refresh by clicking on cancelar?

  • The confirm does not refresh the page... by clicking cancelar the dialog window simply closes.

  • in my code is updating, I can’t figure out why I’m doing it.

  • There must be some other script generating this unwanted behavior.

  • @brunox99x added the scripts I have on the page to the question

  • And this $("#form,#Form1"). Submit(Function(e) ? Is your button not inside a form with id form or Form1? by clicking on the button this script makes the data Submit and reloads the page!

  • When you put a button in a form and do not declare the type it is assumed to be a type="Submit" add type="button" on your buttons so they do not update the page when canceling.

  • @brunox99x when placing type="button" stops updating the page when canceling, but also stops inserting in the database. I solve one problem but create another

  • 1

    So you should not use a form if you want to send data without updating the page and in steps. For this use Ajax.

Show 8 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.