简体   繁体   English

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

[英]Have JavaScript function output variable as string to use in another function

The following JavaScript functions output data plans, the first function myDataPlan is called in the second function dataPlanOutput . 以下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>';
        });
    });
}

Currently I have myDataPlan printing to the div of #dataplan_list within the function. 目前我已将myDataPlan打印到函数内的#dataplan_list的div中。 What I would like it to do is when the function is called it then creates a variable (lets call it printDataPlan )that is added to the final line of the dataPlanOutput so it looks something like this: 我想要它做的是当函数被调用它然后创建一个变量(让我们称之为printDataPlan ),它被添加到dataPlanOutput的最后一行,所以它看起来像这样:

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

Can you just return the value from myDataPlan? 你能从myDataPlan返回值吗? And then execute that at the end of your .innerHTML += within dataPlanOutput? 然后在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');
    });
});
}

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

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