简体   繁体   English

$(function(){})中定义的函数时,Ajax页面onclick不起作用

[英]Ajax page onclick doesn't work when function defined in $(function(){ })

define two functions like this: 定义两个函数,如下所示:

    <script>
        $(function () {
            function testb() {
                alert("ddd");
            }
        })

        function testa() {
            alert("ddd");
        }
    </script>

when page loaded show an ajax page, then call thoese functions from an ajax page, testa will be successful called but not testb . 当页面加载显示ajax页面,然后从ajax页面调用这些函数时,将成功调用testa但不会成功调用testb

If your function is inside the document.ready then you will be able to call global functions as well in ajax success. 如果您的函数在document.ready内部,那么您也可以在ajax成功的情况下调用全局函数。 if your ajax call is in global. 如果您的ajax调用是全局的。 then you will not be able to call a function inside document.ready. 那么您将无法在document.ready内部调用一个函数。 because of scope limitations.. 由于范围限制。

samples: Ajax inside document.ready 样本:document.ready中的Ajax

 function testb() {
    alert('ddd');
}
$(function () {

    $('[id$=btnsubmit]').on('click', function () {

        $.ajax({
            type: "GET",
            dataType: "json",
            url: "/Content/test",
            success: function (data) {
                // alert(data);
                testa();
                testb();
            }
        });

    });
    function testa() {
        alert('zxfgsfg');
    }
});

it will work.. 它会工作..

IF Still you want to call that function in that way, please check below solution: 如果仍然要以这种方式调用该函数,请检查以下解决方案:

    function tesss() {
    alert('ddd');
}

var M7 = {};

$('[id$=btnsubmit]').on('click', function () {
    $.ajax({
        type: "GET",
        dataType: "json",
        url: "/Content/test",
        success: function (data) {
            // alert(data);
            M7();
            tesss();
        }
    });

});
$(function () {
    M7 = function () {
        alert('zxfgsfg');
    };
});

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

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