【问题标题】:"Uncaught ReferenceError: e is not defined" I believe I've defined e“未捕获的 ReferenceError:e 未定义”我相信我已经定义了 e
【发布时间】:2014-12-06 09:39:03
【问题描述】:

我在控制台中收到一条错误消息,提示“未捕获的 ReferenceError:e 未定义”我单击的按钮名称为“sendtxt”。我确定它上面有功能(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);
            }
        });
    };

【问题讨论】:

    标签: ajax referenceerror


    【解决方案1】:

    你没有传递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 是一个函数。无需将其包装在 另一个 函数中。

    【讨论】:

    • 发生在我们所有人身上。 ;)
    • 一些borwsers仍然需要返回false。至少在事件代码的末尾使用其中两个。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多