Reset the button text via Javascript

Asked

Viewed 412 times

1

Well, I don’t know if my title refers to what I’m asking, but I have a form with a Sign in button.

<button id="btn_entrar" data-loading-text="carregando..." autocomplete="off" class="btn btn-primary btn-cons m-t-10" type="submit" onclick="return Get_Acesso();">Entrar</button>

And when I enter login and password I want this button to be "loading..." then I looked it up on the Internet and I saw this case:

  $('button[data-loading-text]').on('click', function () {
    var btn = $(this)
    btn.button('loading')
    setInterval(function () {
        btn.button('reset')
    }, 3000)
});  

But I don’t want a set time ( in case 3 seconds ) I want it to reset after my function GetAcesso() give some return be it false or true. Someone knows a solution?

function Get_Acesso(){
    usuario = document.getElementById('txt_user_name').value;
    senha = document.getElementById('txt_password').value;

    LoginDTO.Usuario = usuario;
    LoginDTO.Senha = senha;

    var DTO = { 'LoginDTO': LoginDTO};
    //acessar login
    $.ajax({
        type: "POST",
        url: "default.aspx/Acesso_Login",
        data: JSON.stringify(DTO),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        beforeSend: function () { 
            $('#btn_entrar').html('Carregando...');
        },          
        success: function (msg) {
            $('#btn_entrar').html('Entrar');
            try {
                //tratamentos de msg
                if (msg.d == "err") {
                    document.getElementById('spn_retorno').innerHTML = "Erro inesperado no momento do acesso.";
                    return false;
                    //Não encontrou nenhum registro
                } else if (msg.d == "not") {
                    document.getElementById('spn_retorno').innerHTML = "Usuário / senha inválido.";
                    return false;
                    //Campos vazios
        }

Simple access code, but I want that when the error return it comes back with the default button of "Enter".

  • Gabriel, put your reference code where you use the function GetAcesso

  • When you press to enter gives some error in Chrome Inspect Element of script ? See there.

  • No error appears

  • Ahh, delete that function. $('button[data-loading-text]')...

  • worked, actually it was on the button itself I took it out of html and it worked "data-loading-text="loading..."". Thanks!

  • All right, that was also for taking away. Good!

Show 1 more comment

2 answers

2


Before the success: function (msg) may place a:

beforeSend: function(){ // Ou seja, 'antes de enviar'
    $('#btn_entrar').html('Carregando...');
},
success: function(msg){
    $('#btn_entrar').html('Entrar'); // Volta o botão no seu valor normal
}
  • I’d be like this ? contenttype: "application/json; charset=utf-8", dataType: "json", beforeSend: Function () { $('#btn_login'). html('Loading...'); }, Success: Function (msg) { $('#btn_login'). html('Log in'); Try {

  • That’s right. What changes is the addition of beforeSend and the addition of Reset button inside the function success.

  • Strange I put the code and it doesn’t change...

  • Updates your POST with the new code.

1

An option would be to make the request via ajax, so you would call the reset function after the reply. Not knowing what this Getaccess function is like, I imagine the best way to do it would be like this:

$.ajax({
    //Os seus parametros para verificar o usuario e senha
        data: parametros,
    //sera usado o método get
        method : 'get',
    //a url que queremos requisitar
        url    : 'requisitada.html',
    //o tipo da requisição
        dataType: 'html',
    //aqui é onde pegamos o retono da requisição
        success : function(retorno){
    //Aqui você coloca a função de resetar o texto do botão.
            ResetaTexto();
        }
    }); 

It was just an example to have at least one basis. I believe it is the best way.

  • I’ll try, includes the Access method if help, thanks for the answer

  • Leonardo, how would this method Resetatexto() ?

  • The example that Zoom gave is a great option of how this method could be!

Browser other questions tagged

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