将JavaScript函数输出变量作为字符串用于另一个函数

时间:2016-12-02 22:34:30

标签: javascript jquery string variables

以下JavaScript函数输出数据计划,第一个函数myDataPlan在第二个函数dataPlanOutput中调用。

function myDataPlan(networkGroupId, countryCode) {
    data_url = apiURL + '/api/v4/networkGroups/' + networkGroupId + '/plansExt?countryCode=' + countryCode;
    jQuery.getJSON(data_url, function (dataPlan) {
        jQuery.each(dataPlan.list, function (i, list) {
            var currencySymbol = getCurrencySymbol(list.currency); 
            //Price
            var content = '<tr>';
            content += '<td>' + currencySymbol + list.price + '</td>';
            //Data Limits
            content += '<td>' + getDataLimit(list) + '</td>';
            //Data Length
            content += '<td>' + list.validityPeriodInDays + '&nbsp;Days</td>';
            content += '</tr>';
            jQuery(content).appendTo("#dataplan_list");
        });
    });
}

function dataPlanOutput(countryCode) {
    document.getElementById("dataplan_list").innerHTML = "";
    network_url = apiURL + '/api/v4/countries/' + countryCode;
    jQuery.getJSON(network_url, function (networkGroup) {
        jQuery.each(networkGroup.list, function (i, list) {
            var countryName = list.region;
            var networkGroupId = list.networkGroupId;
            myDataPlan(networkGroupId, countryCode);
            document.getElementById('dataplan_list').innerHTML += '<tr><td colspan="3" class="title-h4">' + countryName + '</td></tr>';
        });
    });
}

目前我已经myDataPlan打印到函数中#dataplan_list的div。我想要它做的是当函数被调用它然后创建一个变量(让我们称之为printDataPlan),它被添加到dataPlanOutput的最后一行,所以它看起来像这样: / p>

document.getElementById('dataplan_list').innerHTML += '<tr><td colspan="3" class="title-h4">' + countryName + '</td></tr>' + printDataPlan;

1 个答案:

答案 0 :(得分:0)

你能从myDataPlan返回值吗?然后在dataPlanOutput中的.innerHTML +=末尾执行该操作?

function myDataPlan(networkGroupId, countryCode) {
data_url = apiURL + '/api/v4/networkGroups/' + networkGroupId + '/plansExt?countryCode=' + countryCode;
jQuery.getJSON(data_url, function (dataPlan) {
    jQuery.each(dataPlan.list, function (i, list) {
        var currencySymbol = getCurrencySymbol(list.currency); 
        //Price
        var content = '<tr>';
        content += '<td>' + currencySymbol + list.price + '</td>';
        //Data Limits
        content += '<td>' + getDataLimit(list) + '</td>';
        //Data Length
        content += '<td>' + list.validityPeriodInDays + '&nbsp;Days</td>';
        content += '</tr>';
        return content;
    });
});
}

function dataPlanOutput(countryCode) {
document.getElementById("dataplan_list").innerHTML = "";
network_url = apiURL + '/api/v4/countries/' + countryCode;
jQuery.getJSON(network_url, function (networkGroup) {
    jQuery.each(networkGroup.list, function (i, list) {
        var countryName = list.region;
        var networkGroupId = list.networkGroupId;
        myDataPlan(networkGroupId, countryCode);
        document.getElementById('dataplan_list').innerHTML += '<tr><td colspan="3" class="title-h4">' + countryName + '</td></tr>' + myDataPlan('var1','var2');
    });
});
}