在脚本文件中调用第二个函数

时间:2014-09-29 11:31:22

标签: javascript php

var CustomerBoxesLoad = function () {

    var handleAjaxBoxesSelectedCountry = function() {

        $.ajax({
            type: "POST",
            url: 'ajax/selectbox/country_selected.php',
            dataType:'json',
            success: function(data) {

                var select = $("#countryCodeBox"), options = '';
                select.empty();      

                options += "<option value='0'>Select ...</option>";              

                for(var i=0;i<data.length; i++)
                {
                    options += "<option value='"+data[i].id+"'>"+ data[i].name +"</option>";              
                }

                select.append(options);

            }
        }); 

    }

    var handleAjaxBoxesCustomerType = function() {

        $.ajax({
            type: "POST",
            url: 'ajax/selectbox/customer_type.php',
            dataType:'json',
            success: function(data) {

                var select = $("#customerTypeBox"), options = '';
                select.empty();      

                options += "<option value='0'>Select ...</option>";     

                for(var i=0;i<data.length; i++)
                {
                    options += "<option value='"+data[i].id+"'>"+ data[i].name +"</option>";
                }

                select.append(options);

            }
        });     


    }


    return {
        //main function to initiate the module
        init: function () {

        handleAjaxBoxesSelectedCountry();
        handleAjaxBoxesCustomerType();

        }

    };

}();

我有一个从PHP文件调用的函数。在PHP文件的末尾,我调用了Ajax函数:

    jQuery(document).ready(function() {    
        CustomerBoxesLoad.init();   
    });

所有Ajax选择框填充效果都很好。现在我想从(a href =&#34;&#34;)标签触发Ajax脚本。但是这个功能不会像这样触发:

a href="javascript:handleAjaxBoxesSelectedCountry();">Refresh Select Box

这对JavaScript的引用不起作用!!

2 个答案:

答案 0 :(得分:0)

为你的回归添加另一个功能:

 return {
    //main function to initiate the module
    init: function () {
        handleAjaxBoxesSelectedCountry();
        handleAjaxBoxesCustomerType();
    }

    handleAjaxBoxesSelectedCountry: function () {
        handleAjaxBoxesSelectedCountry();
    }
};

和&#39; a&#39;标记:

<a href="javascript:CustomerBoxesLoad().init();return false;">STH</a>

答案 1 :(得分:0)

我自己解决了这个问题:

return {

    //main function to initiate the module
    init: function () {

        handleAjaxBoxesCountry();
        handleAjaxBoxesCustomerType();

    },
    init_country: function () {
        handleAjaxBoxesCountry();
    },
    init_type: function () {
        handleAjaxBoxesCustomerType();
    }       

};