当用户单击提交按钮或回车时提交

时间:2018-06-14 01:19:14

标签: javascript jquery

我想在用户点击Enter或提交按钮时提交表单ajax这是我的代码:

$('.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");
});

提前致谢

1 个答案:

答案 0 :(得分:0)

你试试:

html代码:

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

javascript代码:

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();
    }
});