简体   繁体   中英

laravel - How to trigger enter key press of textbox to implement javascript code

I want to use enter key to implement jquery code, if I used on blur its working fine but on using key event doesnt implement the code. Im using datatables and for one column I use inputtext for display data

datatable :

  {data: 'vendor_name', name: 'vendors.vendor_name',mRender: function (data, type, row) {                   
return '<input type="text" id="vendor_name" 
   class="vendor_name" data-id="'+row.id+'" onkeydown="myFunction(event)" 
     onClick="this.select();"  value="'+data+'">';}},

javascript

 <script type="text/javascript">
  function myFunction(event) {
 var x = event.keyCode;
var id = $(this).data("id");
 var $row = $(this).closest('tr');
   if (x == 13) 
 { 
    $.ajax({
        type: 'POST',
        url: 'Vendor_Save',
        data: {
            '_token': $('input[name=_token]').val(),
            'currentid': id,
           'current_vendor_name':$(this).val(),
        },
        success: function(data) {
       $row.closest('tr').find('.category_name').val(data.categoryname); 
     $row.closest('tr').find('.vendor_no').val(data.vendorid);                        
        }
     });
     }
        };
    </script>

you should check the keyupevent

 $("input#vendor_name").keyup(function(event) {
        if (event.keyCode === 13) {

               //call you ajax() here
        }
    });

I tried following and working good to me.

<script type="text/javascript">
$("#vendor_name").keyup(function (e) {
    if (e.keyCode == 13) {
        // AJAX Call
        $.ajax({
           url : YOUR_URL,
           method : "POST",
           data : JSON_DATA,
           dataType : "text",
           success : function (data)
           {
              console.log(data);
           }
       });
    }
});
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM