2

I'm trying to get the button to work without clicking on it.

I tried but with using 'Submit' and 'button', neither works unless user clicks the button.

 <input id="txtDusNumber" class="form-control form-control-sm" type="text" />
 <input type="submit" name="submit" class="btn " value="Search" id="enter-btn" onclick="ShowCard()" />
 <input type="button" class="btn " value="Search" onclick="ShowCard()" />


<script>
function ShowCard() {
        var dusNumberVal = $('#txtDusNumber').val();
        if (dusNumberVal) {
                $.ajax({
                        url: "@Url.Action("display","Card")",
                        data: { number: dusNumberVal },
                        type: 'GET',
                        success: function (result) {
                                $('#results').html(result);
                        },
                        error: function () {
                                alert("error...");
                        }
                });
        } else {
                alert('Please enter  Number.');
        }
}
</script>
YakovL
  • 7,557
  • 12
  • 62
  • 102
anatp_123
  • 1,165
  • 2
  • 10
  • 25
  • 1
    Possible duplicate of [Jquery: how to trigger click event on pressing enter key](https://stackoverflow.com/questions/18160342/jquery-how-to-trigger-click-event-on-pressing-enter-key) – Nikhil Kinkar Dec 14 '18 at 22:17
  • Just add a onkeypress to the button. Something like this- – Rohit Nethi Dec 14 '18 at 22:27

2 Answers2

2

Try this:

$(document).keypress(function(e) {
    if(e.which == 13) {
        ShowCard();
    }
});


function ShowCard(){
...
}

What it does is when the page is loaded, once you press enter, the ShowCard function gets executed.

Falcon
  • 837
  • 1
  • 9
  • 28
1

Wrap <button type="submit"> in <form> tag

colorswall
  • 188
  • 1
  • 4