Home > Back-end >  execute ajax after enter and press button
execute ajax after enter and press button

Time:06-27

i have easy ajax script, which send data from form, it work this button, and i want to date send when i press enter, how i can make it? Very thanks!

ajax code

jQuery(document).ready(function(){
    jQuery('.ajaxSubmit').click(function(e) {
        e.preventDefault();
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
            }
        });
            jQuery.ajax({
                url: "{{ url('/names') }}",
                method: 'get',
                data: {
                    runBy: $(this).closest("form").find(".name").val(),
                    command: $(this).closest("form").find(".surname").val(),
                },
                success: function() {
                    location.reload();
                }
            });
    });
});

php code

  <form>
       <input type="hidden" value="{{Auth::user()?->name}}" >
        <textarea  name="input" cols="30" rows="1"> </textarea>
        <button  onclick="event.preventDefault()">send</button>
  </form>

CodePudding user response:

Move your Ajax submission code to a separate function. Add the Keypress event to the input element. Call that function from the keypress event when the keypress value is 13. Below is the code for your reference.

$('.input').keypress(function (e) {
  if (e.which == 13) {
    // Call the callback function
    return false;
  }
});

call the same Ajax function from a button click.

Regards, omi

  • Related