How to take data from two inputs without using form?

Asked

Viewed 40 times

1

I want to do something like in Excel. I have two inputs and in each I type values. By pressing ENTER I want to capture the entered values, how can I do this? OBS: I’m using Angular 1.

<div class="descontoPromo">
    <input class="form-control depro" type="text" name="descontoPromocional" ng-model="valor.descPromo" >
</div>
<div class="descontoFin">
    <input class="form-control defin" type="text" name="descontoFinanceiro" ng-model="valor.descFinan" >
</div>
  • You could use ngModel for that, as I understand it, you should just use the values of valor.descPromo and valor.descFinan

  • This is if I use a function flame on a button in a form, but I don’t want to use a form or button.

  • You don’t want a button, because you want the function to be called by enter, or you have another reason? Because if it is by enter, da para colocar um botão oculto, e que tratar no Js para fazer o enter execute o clique do botão.

  • Exactly, "do not want button, because you want the function to be called by enter". And how to do this treatment in JS to run the button?

  • published the answer with an example of how to do it. I remembered that I use it without having this invisible button. I hope it helped you.

1 answer

2


Just capture the keydown event from $document of Angularjs, and call the function you want to be executed.

I demonstrate how to do this with the code below. both the function and the eventListener must be in the controller of the html you want to execute the function by enter.

This way you won’t need a button, or even a form.

    function enter(e) {
        var keyCode = e.keyCode;
        if (keyCode == 13) {
            suaFuncao()
        } 
    }

    $document[0].addEventListener("keydown", enter, false);
  • I’ll try, thank you.

  • Why $Document[0]?

  • in my case @Document was returning me an array with 1 item, so I had to do this.

Browser other questions tagged

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