无法在按钮单击时调用jQuery函数

时间:2017-10-17 13:06:58

标签: javascript jquery

点击保存按钮时尝试调用jQuery函数。但是我点击按钮时没有得到任何响应。

<button type="button" id="save" class="btn btn-primary pull-right">Save Expense</button>
$('#save').on('click', function() {
  alert("test");
  var data = $('form').serialize();
  console.log("data" + data);
  $.ajax({
    type: "POST",
    url: "saveExpense",
    data: data,
    cache: false,
    success: function(data) {
      alert("Success");
    }
  });
});

1 个答案:

答案 0 :(得分:0)

像这样创建一个点击事件,这将解决您的问题。

&#13;
&#13;
$(document).on('click','#save', function() {
      alert("test");
      var data = $('form').serialize();
      console.log("data" + data);
      $.ajax({
        type: "POST",
        url: "saveExpense",
        data: data,
        cache: false,
        success: function(data) {
          alert("Success");
        }
      })
})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button type="button" id="save" class="btn btn-primary pull-right">Save Expense</button>
&#13;
&#13;
&#13;