2014-12-06 390 views
0

我在控制檯發生錯誤,提示「Uncaught ReferenceError:e未定義」我點擊的按鈕名稱爲「sendtxt」。我確定它有它的功能(e)「Uncaught ReferenceError:e is not defined」我相信我已經定義了e

<script type="text/javascript"> 
    $(document).ready(function() { 


     $('input[name="sendtxt"]').click(function(e) { 
      sendText(); 
     }); 

    }); 
    /************ FUNCTIONS ******************/ 

    function sendText() { 
     e.preventDefault(); 
     var phonenum = $('input[name="phonenum"]').val(); 
     var provider = $('select[name="provider"]').val(); 
     $.ajax({ 
      type: 'POST', 
      data: { 
       provider: provider, 
       phonenum: phonenum 
      }, 
      url: 'send.php', 
      success: function(data) { 
       console.log('Success'); 

      }, 
      error: function(xhr, err) { 
       console.log("readyState: " + xhr.readyState + "\nstatus: " + xhr.status); 
       console.log("responseText: " + xhr.responseText); 
      } 
     }); 
    }; 

回答

0

你沒有通過e

$(document).ready(function() { 
    $('input[name="sendtxt"]').click(function(e) { 
     sendText(e); // <<< 
    }); 
}); 
/************ FUNCTIONS ******************/ 

function sendText(e) { // <<< 
    e.preventDefault(); 
} 

但實際上,被寫爲更容易:

$(function() { 
    $('input[name="sendtxt"]').click(sendText); 
}); 
/************ FUNCTIONS ******************/ 

function sendText(e) { 
    e.preventDefault(); 
} 

jQuery的事件處理程序期望的功能,並且sendText是一個函數。無需將其包裝在的另一個功能中。

+0

我是個白癡。非常感謝你 – DanMossa 2014-12-06 09:44:21

+0

適合所有人。 ;) – Tomalak 2014-12-06 09:46:32

+0

一些borwsers仍然需要返回false。至少在事件代碼的末尾使用其中的兩個。 – littlealien 2014-12-06 09:47:26

相關問題