-2

如何在链接中添加 javascript 变量?下面的代码产生一个空白页。viewData.php 将包含 PHP GET 变量来获取周期和类型,以确定我是拉租金还是销售金额。期间将采用 yyyymm 格式。

$(document).ready(function(){
init();
 });
 function init(){
 months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
 urls = ['http:/testServer/testPage/viewData.php?period=' + series.name + ''];

$.ajax({
url: 'readData.php'
}).done(function(data) {
    ar = data.split('###');
    for(var i=0;i<ar.length;i++){
        ar[i] = ar[i].split('##');
    }
    for(var i=0;i<ar[0].length;i++){
        text = ar[0][i].split(' ').join('');
        year = text.substring(0,4);
        month = parseInt(text.split(year).join(''));
        month = months[month-1];
        ar[0][i] = month +','+year;
        ar[1][i] = parseFloat(ar[1][i]);
        ar[2][i] = parseFloat(ar[2][i]);
    }
    count = 0;
    dates = [];
    dates.push(ar[0][0]);
    for(var i=1;i<ar[0].length;i++){
        count++;

            dates.push(ar[0][i]);


    }
    dates[dates.length-1] = ar[0][dates.length-1];
    createGraph(ar,dates);
}); 
 }
   function createGraph(ar,dates){

        $('#60MonthAmount').highcharts({

        chart: {
            type: "line"
                },

        title: {
            text: '60 Month Revenue by Location Chart'
        },
        subtitle: {
            text: ''
        },
        xAxis: {
            title: {
                text: 'Time Period'
            },
            labels: {
                formatter: function() {
                    return this.value; // clean, unformatted number for year
                }
            },
            categories: dates,
            minTickInterval: 6,
            showLastLabel: true,


        },
        yAxis: {
            title: {
                text: ''
            },
            min: 0,
            labels: {
                formatter: function() {
                    return this.value / 1000000 +' mil';
                }
            }
        },
        tooltip: {
            pointFormat: '{series.name} produced <b>${point.y:,.0f}</b><br/><p style="visibility: hidden;">_</p>'
        },
        plotOptions: {
        series: {
            cursor: 'pointer',
            point: {
                events: {
                    click: function() {
                        if(this.x>urls.length){
                            url = urls[0];
                        }else{
                            url = urls[this.x];
                        }
                        window.open(url, '_blank');
                    }
                }
            }
        }
        },
        series: [{
            name: 'Rentals',
            data: ar[1]
        }, {
            name: 'Sales',
            data: ar[2]
        }]
    });
}
4

1 回答 1

1

如何在链接中添加 javascript 变量?

我不知道是什么series.name,但是要确保您的 URL 有效,您需要encodeURIComponent在任何 url 参数上使用,以便它们得到正确编码。

var url = 'http:/testServer/testPage/viewData.php?period=' + encodeURIComponent(series.name);
于 2013-10-23T20:55:27.610 回答