未捕获的ReferenceError:函数未定义jQuery [英] Uncaught ReferenceError: function is not defined jQuery

查看:102
本文介绍了未捕获的ReferenceError:函数未定义jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在单击按钮时调用jQuery函数。但我得到的错误如下:

I am trying to call jQuery function when button is clicked. But I am getting the error as follows:

未捕获的ReferenceError:未定义update_question_ajax

Uncaught ReferenceError: update_question_ajax is not defined

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:

$(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.

谢谢!

推荐答案

似乎是一个范围问题。您已经在另一个函数中定义了该函数。你还应该尽量避免将php放在javascript中。从技术上讲,它有效,但形式并不是很好。

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. 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>

然后我们创建一个自执行函数,将jQuery传递给$,它提供了使用内容的所有内容$作为jQuery。然后定义按钮的单击处理程序并使用数据属性传递id,而不是使用JS混合在JS中并将其放在DOM中。只是感觉有点清洁。此外,请确保在文档底部加载jquery /其他脚本,就在关闭正文之前。这样您就不需要.ready,因为您的文档已经加载。

Then we create a self executing function, pass jQuery in as $ which gives everything inside the ability to use "$" as 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. 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. This way you don't need a .ready because your document will already be loaded.

(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));

这篇关于未捕获的ReferenceError:函数未定义jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆