Problem with Onkeypress and Onenterkey

Asked

Viewed 238 times

0

I have a problem, I need to get the value typed in a field, I thought to use OnKeyPress calling the function key_press as follows:

        var str;
        var keycode;
     function key_press(e) {
                var event = e.event, keyCode = event.key || String.fromCharCode(event.which);
                if (keyCode != "Enter") {
                    event = e.event, str = event.key || String.fromCharCode(event.which);
                    if (/^[\.\,e]$/.test(str))
                        event.preventDefault();
                }
        }

So far everything ok, he performs it the right way, I added the .OnEnterKey("enter") for it to perform the following function :

        function enter() {
                for (var i = 0; i < parseInt(str); i++) {
                    $("#gridContainer2").dxDataGrid("addRow");
                }

        }

What’s wrong with my job key_press it takes the value that is passed every time a number is typed, ie if I type 1 it picks up normal, if I type 8 too, but when I step 2 numbers this is no longer right, because the var str now receives the second digit I put; ex: I typed 23 and gave Enter to var str receives only the 3, because executed the function key_press and received 3, how can I get all the value of the field?? ex: typed 15, I want that str receive 15 and run the 0 to 14, I’ve been researching and told to use the following : document.getElementById("ID DO ELEMENTO").value, but every time I use it, I get value undefined

My field on the form:

@(Html.DevExtreme().Form<FormaPagamentoViewModel>
    ()
    .ID("formularioCadastro")
    .ShowValidationSummary(false)
    .Items(items =>
    {
    items.AddGroup()
    .Items(groupItems =>
    {
    groupItems.AddSimpleFor(m => m.Fpg_quantidade)
    .Editor(e => e.NumberBox().ID("qua").Width("70px").OnKeyPress("key_press").OnEnterKey("enter")); //esse campo que estou usando
    });
    })
    .FormData(Model)
    )
  • stores and concatenates what you type into an auxiliary var, then, in enter, you go to auxiliary. Understood?

  • The ideal would be to call the function in the Blur, Leave or even leave a range in the keyUp to identify which user stopped typing and whether the value is valid

  • @cpll, it’s a good idea, but I don’t know how to put in an auxiliary variable the values

  • @Leandroangelo I don’t know how to do the things you said

1 answer

3


Why do you capture the value of the input every time the user presses a key if you will only use it when hit enter?

It would not be enough to capture this value once in the enter function?

function enter(e) {
    var str = e.event.path[0].value;
    for (var i = 0; i < parseInt(str); i++) {
        $("#gridContainer2").dxDataGrid("addRow");
    }
}

Note: I don’t know the method you used to add Event Listener, the way I transferred the value of the input is just a deduction based on the code you posted.

I did some research and they said to use the following : Document.getElementById("ELEMENT ID"). value, but whenever I use that, I get Undefined value

You’re probably using it the wrong way, but there’s no telling without the code.

  • I’ll test it tomorrow morning, because the code is on the company’s pc, but I need to perform that code every time I type a key to verify characters, but I didn’t know any other way to get data, I’ll test this way tomorrow

  • This way you told me, I can get the second element, for example, if I type 10, I get the 0, if I type 15, I get the 5, I want to post the whole code?

Browser other questions tagged

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