2014-12-06 388 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

相关问题