Enter Button in Chrome and Firefox

0

I need to press the enter key when the user presses the button. I'm doing it this way, but it's not working in Google Chrome or firefox:

     if (!IsPostBack)
            {
                txtsenha.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('"+btnEntrar.UniqueID+"').click();return false;}} else {return true}; ");
            }

I also tried putting it this way, but it also does not work in Google Chrome and firefox.

  <asp:Panel ID="Panel2" runat="server" DefaultButton="btnEntrar">
                        <div class="col-xs-6">
                            <div class="input-group">
                                <asp:TextBox ID="Empresa" class="form-control" runat="server" placeholder="Código da Empresa"></asp:TextBox>
                                <span class="input-group-addon">
                                    <button class="fa fa-industry" style="background: transparent; border: none"></button>
                                </span>
                            </div>
                        </div>
                        <br />
                        <div class="col-xs-7">
                            <div class="input-group">
                                <asp:TextBox ID="txtIdent" class="form-control" runat="server" placeholder="Identificador"></asp:TextBox>
                                <span class="input-group-addon">
                                    <button class="fa fa-user fa-lg" style="background: transparent; border: none"></button>
                                </span>
                            </div>
                        </div>
                        <br />
                        <br />
                        <div class="col-xs-8">
                            <div class="input-group">
                                <asp:TextBox ID="txtsenha" class="form-control" runat="server" type="password" placeholder="Senha" AutoPostBack="False" OnTextChanged="txtsenha_TextChanged"></asp:TextBox>
                                <span class="input-group-addon">
                                    <button class="fa fa-unlock-alt fa-lg" style="background: transparent; border: none"></button>
                                </span>
                            </div>
                        </div>
                        <br />

                        <asp:Button ID="btnEntrar" runat="server" Text="Entrar " CssClass="radius" OnClick="btnEntrar_Click" />
                    </asp:Panel>

I made this adaptation in the code, but nothing happens either.

    <script type="text/javascript">
        jQuery(document).ready(function () {
            jQuery('#txtsenha').on('keypress', function (e) {
                if (e.keyCode == '13') {
                    jQuery('#btnEntrar').trigger('click');
                }
            })

            jQuery('#btnEntrar').click(function () {
                alert('Click!');
            });

        });
    </script>

<div class="col-xs-8">
                        <div class="input-group">
                            <asp:TextBox ID="txtsenha" class="form-control" runat="server" type="password" placeholder="Senha" AutoPostBack="False" OnTextChanged="txtsenha_TextChanged"></asp:TextBox>
                            <span class="input-group-addon">
                                <button class="fa fa-unlock-alt fa-lg" style="background: transparent; border: none"></button>
                            </span>
                        </div>
                    </div>
                    <br />
                    <asp:Button ID="btnEntrar" runat="server" Text="Entrar " CssClass="radius" OnClick="btnEntrar_Click" />
    
asked by anonymous 12.06.2017 / 14:55

2 answers

0

I was able to solve this code. Thanks to all who helped.

 if (!IsPostBack)
            {
                txtsenha.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('" + btnEntrar.ClientID + "').click();return false;}} else {return true}; ");
            }
    
13.06.2017 / 17:55
1

Below is a working example using jQuery.

jQuery(document).ready(function(){
  jQuery('#senha').on('keyup',function(e){
    if(e.keyCode == '13'){
      jQuery('#entrar').trigger('click');
    }
  })
  
  jQuery('#entrar').click(function(){
    alert('Click!');
  });
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><inputname="senha" id="senha" value="" type="password">
<input type="button" value="entrar" name="entrar" id="entrar">
    
12.06.2017 / 16:43