簡體   English   中英

JavaScript函數參考錯誤

[英]Javascript function reference error

我正在嘗試通過Rails應用程序上的命令按鈕調用Javascript函數。

function fetchpurchases() {
  var startDate = $('#histStart').val();
  var endDate = $('#histEnd').val();
  $('#histMainFrame').empty();
  $.ajax({
    url: '/transHistory/confirm?start_date=' + startDate + '&end_date=' + endDate,
    type: 'get',
    beforeSend: function() {
      $('#mainContDiv').append('<img id=\'spinner\' src=\'/assets/spinner_green.gif\'>');
    },
    success: function(output) {
      $('#spinner').remove();
      $('#histMainFrame').html(output);
      $('#transPurTab').DataTable({
        destroy: true,
        lengthChange: false,
        pageLength: 50,
        paging: true,
        stripeClasses: ['oddStrip','evenStrip']
      });
    }
  });
}

通過調用Javascript函數的onclick事件定義了一個按鈕。

<div id="histToolbar">
  <div id="errorDiv"><p class="bigRedBright"></p></div>
  <table id="histTools">
    <tr>
      <th>Start Date</th>
      <th>End Date</th>
      <th></th>
    </tr>
    <tr>
        <td><input type="text" class="datepicker" id="histStart" placeholder="Start Date..."></td>
        <td><input type="text" class="datepicker" id="histEnd" placeholder="End Date..."></td>
        <td><button onclick="fetchHistory()" id="histSubmit">Submit</button></td>
        <td><button onclick="fetchpurchases()" id="deliverSubmit">Confirm Delivery</button></td>
    </tr>
  </table>
</div>

我可以通過在瀏覽器中手動鍵入頁面來導航到我創建的頁面。 我編寫的MySQL查詢工作正常,但是當我嘗試使用按鈕進行導航時,出現reference error: fetchpurchases is not defined

另外(可能不相關),當我嘗試使用Firebug調試錯誤時,該功能不會顯示在DOM窗口中。

我會做這樣的事情

// Create a namespace for your code
var App = App || {};

(function() {
  App.fetchpurchases = function() {
    //Your code here
  };
})();

如果您不想使用JS或jQuery將函數綁定到事件,則可以將HTML更改為:

<td><button onclick="App.fetchpurchases()" id="deliverSubmit" type="button">Confirm Delivery</button></td>

當然,無論您放置在哪里,都需要將javascript添加到application.js或手動將其加載到頁面中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM