点击更改值和运行功能

时间:2013-03-31 12:47:50

标签: jquery highcharts

function ShowChart() {
    var fromDate = $("#fromdate").val().replace(",", "");
    var toDate = $("#todate").val().replace(",", "");
    var chartType = 'Customers'; // Revenue,Refunds,Customers,Purchases
    var chartURL = "<?php echo base_url() . 'merchant/GetChartData/'; ?>";
    chartURL += chartType + "/" + fromDate + "/" + toDate + "/";
}
// run it
ShowChart();

HTML:

<ul class="tabs">
    <li><a href="#Revenue">Revenue</a></li>
    <li><a href="#Refunds">Refunds</a></li>
    <li><a href="#Customers">Customers</a></li>
    <li><a href="#Purchases">Purchases</a></li>
</ul>

这是我可以通过手动更改chartType来显示图表的图表设置,但我想自动执行此操作,因此单击链接将更新图表。

我知道可以使用var str = this.href.split("#")[1];读取和存储href值,但我不确定如何“更改”chartType的值并在每次点击时“运行”该功能?

1 个答案:

答案 0 :(得分:1)

尝试使用.click()将ShowChart功能附加到使用<a>选择器的每个.tabs a标记的点击事件中。然后,只需将chartType设置为您的split代码。

function ShowChart() {
    var fromDate = $("#fromdate").val().replace(",", "");
    var toDate = $("#todate").val().replace(",", "");
    var chartType = this.href.split("#")[1]; // Revenue,Refunds,Customers,Purchases
    var chartURL = "<?php echo base_url() . 'merchant/GetChartData/'; ?>";
    chartURL += chartType + "/" + fromDate + "/" + toDate + "/";
}

// Attach event to every a under .tab    
$('.tabs a').click(ShowChart);