-2

I want to submit a form ajax when the user clicks on the Enter or the Submit Button this is my code :

$('.setter').on('submit keypress', function(){
   $.post(action,$(this).serialize(), function(data){
       window[data.jsFunction](data); // or get it form CI as an array data
   }, "json");
});

thanks in advance

muecas
  • 4,200
  • 1
  • 7
  • 18
  • if a form has a submit button enter should call submit.... You are also not cancelling the default submit action of the form. – epascarello Jun 14 '18 at 01:20
  • So why is that code not working? What is wrong with it? – epascarello Jun 14 '18 at 01:41
  • Duplicate for pressing enter key: https://stackoverflow.com/questions/699065/submitting-a-form-on-enter-with-jquery I didn't flag it as full duplicate since you also want to use an onclick function. – hungrykoala Jun 14 '18 at 02:20

1 Answers1

0

You try:

html code:

 <button type="button" id="btnSubmit" onclick="postData()">Submit</button>

javascript code:

function postData(){
  $.post(action,$(this).serialize(), function(data){
       window[data.jsFunction](data); // or get it form CI as an array data
   }, "json");
}

$(document).keypress(function(e) {
    if(e.which == 13) {
       postData();
    }
});
Tran Audi
  • 569
  • 1
  • 6
  • 19