简体   繁体   English

未捕获的ReferenceError:函数未定义jQuery

[英]Uncaught ReferenceError: function is not defined jQuery

I am trying to call jQuery function when button is clicked. 我试图在单击按钮时调用jQuery函数。 But I am getting the error as follows: 但我得到的错误如下:

Uncaught ReferenceError: update_question_ajax is not defined 未捕获的ReferenceError:未定义update_question_ajax

HTML: HTML:

<button type="button" class="update-question-<?php echo $i; ?> button" onclick="update_question_ajax(<?php echo $i; ?>)" style="outline: 0 none;"><?php _e('Update') ?></button>

jQuery: jQuery的:

$(function(){
    function update_question_ajax(id)
    {
        var test = $('.edit-question-' + id + ' input[name="translated"]');
        var editedQuestionId = $('#question-id-'+id).val();
        var editedQuestionObj = $('.edit-question-' + id + ' input[name="translated"]').val();
        var modalObj = $('#myQuestionModal');

        $.ajax({
            type: "POST",
            url: "<?php echo base_url('admin/question/admin_edit_question'); ?>",
            data:{
                edited_question: editedQuestionObj,
                question: editedQuestionId
            },
            success: function(){
                modalObj.dialog('close');
                modalObj.html('');
            },
            complete: function(){
                //window.location.reload(true);
            }
        });
        return false;
    }
});

I appreciate if you guys help me out about this. 如果你们帮我解决这个问题,我感激不尽。

Thank you! 谢谢!

Seems to be a scope issue. 似乎是一个范围问题。 You've defined the function essentially inside another function. 您已经在另一个函数中定义了该函数。 You should also try to avoid placing php inside javascript. 你还应该尽量避免将php放在javascript中。 Technically it works, but isn't really good form. 从技术上讲,它有效,但形式并不是很好。

Removed the inline click handler. 删除了内联单击处理程序。

<button type="button" class="update-question button" data-id="<?php echo $i; ?>" style="outline: 0 none;"><?php _e('Update') ?></button>

Then we create a self executing function, pass jQuery in as $ which gives everything inside the ability to use "$" as jQuery. 然后我们创建一个自执行函数,以$ $形式传递jQuery,它提供了使用“$”作为jQuery的能力。 Then define your click handler for the button and use a data attribute to pass the id rather than using PHP mixed in your JS and placing it in the DOM. 然后定义按钮的单击处理程序并使用数据属性传递id,而不是使用JS混合在JS中并将其放在DOM中。 Just feels a little cleaner. 只是感觉有点清洁。 Also, make sure you are loading jquery / other scripts at the bottom of your document, just before the close body. 此外,请确保在文档底部加载jquery /其他脚本,就在关闭正文之前。 This way you don't need a .ready because your document will already be loaded. 这样您就不需要.ready,因为您的文档已经加载。

(function($){
    // Define click handler. 
    $('button.update-question').on('click', function(e){
        e.preventDefault();
        var id = $(this).data('id');

        update_question_ajax(id);
    });

    function update_question_ajax(id) {
        var test = $('.edit-question-' + id + ' input[name="translated"]'),
            editedQuestionId = $('#question-id-'+id).val(),
            editedQuestionObj = $('.edit-question-' + id + ' input[name="translated"]').val(),
            modalObj = $('#myQuestionModal');

        $.ajax({
            type: "POST",
            url: "YOURURL",
            data:{
                edited_question: editedQuestionObj,
                question: editedQuestionId
            },
            success: function(){
                modalObj.dialog('close');
                modalObj.html('');
            },
            complete: function(){
                //window.location.reload(true);
            }
        });
    }

}(jQuery));

You should move the function definition outside of the document.ready callback ($(function() { ... } ): 你应该将函数定义outside的的document.ready回调($(function() { ... }

function update_question_ajax(id) {
    var test = $('.edit-question-' + id + ' input[name="translated"]');
    var editedQuestionId = $('#question-id-'+id).val();
    var editedQuestionObj = $('.edit-question-' + id + ' input[name="translated"]').val();
    var modalObj = $('#myQuestionModal');

    $.ajax({
        type: "POST",
        url: "<?php echo base_url('admin/question/admin_edit_question'); ?>",
        data:{
            edited_question: editedQuestionObj,
            question: editedQuestionId
        },
        success: function(){
            modalObj.dialog('close');
            modalObj.html('');
        },
        complete: function(){
            //window.location.reload(true);
        }
    });
    return false;
}

Everything you put inside the $(function() {} is executed when the DOM is ready but is private to its scope. 你在$(function() {}放置的所有东西都是在DOM准备就绪时执行的,但是它的作用域是私有的。

If on the other hand you want to use unobtrusive javascript, then get rid of this onclick attribute from your markup: 另一方面,如果你想使用不引人注目的javascript,那么从你的标记中删除这个onclick属性:

<button type="button" class="button update-question-<?php echo $i; ?>" dtaa-id="<?php echo $i; ?>" style="outline: 0 none;"><?php _e('Update') ?></button>

and then use the document ready callback to unobtrusively subscribe to the .click event of those buttons: 然后使用文档就绪回调来不引人注意地订阅这些按钮的.click事件:

$(function() {
    function update_question_ajax(id) {
        var test = $('.edit-question-' + id + ' input[name="translated"]');
        var editedQuestionId = $('#question-id-'+id).val();
        var editedQuestionObj = $('.edit-question-' + id + ' input[name="translated"]').val();
        var modalObj = $('#myQuestionModal');

        $.ajax({
            type: "POST",
            url: "<?php echo base_url('admin/question/admin_edit_question'); ?>",
            data:{
                edited_question: editedQuestionObj,
                question: editedQuestionId
            },
            success: function(){
                modalObj.dialog('close');
                modalObj.html('');
            },
            complete: function(){
                //window.location.reload(true);
            }
        });
        return false;
    }


    $('.button').click(function() { 
        var id = $(this).data('id');
        return update_question_ajax(id);
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM